Set to List in Java

10 Jun, 2022
Given a set (HashSet or TreeSet) of strings in Java, convert it into a list (ArrayList or LinkedList) of strings. In java, Set interface is present in java.util package and extends the Collection interface is an unordered collection of objects in which duplicate values cannot be stored. List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. 
Input :  Set hash_Set = new HashSet();
         hash_Set.add("Geeks");
         hash_Set.add("For");
        
Output : ArrayList or Linked List with 
         following content
         {"Geeks", "for"}
There are numerous approaches to do the conversion of a Set to List in Java. A few of them are listed below.
Using Set TraversingUsing ArrayList or LinkedList ConstructorUsing addAll() methodUsing Stream in JavaUsing List.copyOf() in java
Using Set Traversing
Using ArrayList or LinkedList Constructor
Using addAll() method
Using Stream in Java
Using List.copyOf() in java
We simply create a list. We traverse the given set and one by one add elements to the list.
Java
// Java program to demonstrate conversion of// Set to array using simple traversal import java.util.*; class Test {    public static void main(String[] args)    {         // Creating a hash set of strings        Set<String> s = new HashSet<String>();        s.add("Geeks");        s.add("for");         int n = s.size();        List<String> aList = new ArrayList<String>(n);        for (String x : s)            aList.add(x);         System.out.println("Created ArrayList is");        for (String x : aList)            System.out.println(x);         // We can create LinkedList same way    }}
Created ArrayList is
Geeks
for

We can simply convert a Set into a List using the constructor of an ArrayList or LinkedList.
Java
// Java program to demonstrate conversion of// Set to list using constructor import java.util.*; class Test {    public static void main(String[] args)    {         // Creating a hash set of strings        Set<String> s = new HashSet<String>();        s.add("Geeks");        s.add("for");         // Creating an array list using constructor        List<String> aList = new ArrayList<String>(s);         System.out.println("Created ArrayList is");        for (String x : aList)            System.out.println(x);         System.out.println("Created LinkedList is");        List<String> lList = new LinkedList<String>(s);        for (String x : lList)            System.out.println(x);    }}
Created ArrayList is
Geeks
for
Created LinkedList is
Geeks
for

The Set to List conversion in Java can also be performed using the addAll() method of Java List.
Java
// Java program to demonstrate conversion of// Set to array using addAll() method. import java.util.*; class Test {    public static void main(String[] args)    {         // Creating a hash set of strings        Set<String> s = new HashSet<String>();        s.add("Geeks");        s.add("for");         List<String> aList = new ArrayList<String>();        aList.addAll(s);         System.out.println("Created ArrayList is");        for (String x : aList)            System.out.println(x);         List<String> lList = new LinkedList<String>();        lList.addAll(s);         System.out.println("Created LinkedList is");        for (String x : lList)            System.out.println(x);    }}
Created ArrayList is
Geeks
for
Created LinkedList is
Geeks
for

We use the stream in Java to convert the given set to steam, then stream to list. This works only in Java 8 or versions after that.
Java
// Java program to demonstrate conversion of// Set to list using stream import java.util.*;import java.util.stream.*; class Test {    public static void main(String[] args)    {         // Creating a hash set of strings        Set<String> s = new HashSet<String>();        s.add("Geeks");        s.add("for");         List<String> aList            = s.stream().collect(Collectors.toList());         for (String x : aList)            System.out.println(x);    }}
Geeks
for

We can convert the set to list by using List.copyOf(object) in java. For this, we have to import the package java.util.List.* It is a static factory method which creates the list object from another collection or object. 
Java
import java.io.*;import java.util.*; class GFG {    public static void main(String[] args)    {        Set<String> s = new HashSet<String>();        s.add("geeks");        s.add("forgeeks");        s.add("learning");        s.add("platform");         List<String> aList = new ArrayList<>();        aList = List.copyOf(s);         System.out.println("Created ArrayList is :"                           + aList);    }}
Created ArrayList is :[geeks, forgeeks, learning, platform]

nishkarshgandhi
keerthikarathan123
Java-ArrayList
Java-Collections
java-hashset
java-LinkedList
java-list
Java-List-Programs
Java-Set-Programs
java-stream
Java-Stream-programs
java-treeset
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Stream In Java
ArrayList in Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Stack Class in Java
Initialize an ArrayList in Java
Initializing a List in Java
Introduction to Java