How to Convert Set to List in Java
Introduction to Convert Set to List
Lists in Java are ordered collections of data, whereas sets are unordered collections of data. A list can have duplicate entries, but a set cannot. Both data structures are useful in different scenarios.
Knowing how to convert a set into a list is useful as it transforms unordered data into ordered data.
Initializing a Set
Let’s initialize a set and add some elements to it.
import java.util.*;
public class Main {
public static void main(String[] args)
{
Set<Integer> a = new HashSet<>();
a.add(1);
a.add(2);
a.add(3);
a.add(1);
System.out.println(a);
}
}
The add()
method of HashSet
adds elements to the set. Note that the elements are distinct. Sets are unordered, so there’s no way to get elements by their insertion order.
Convert Set to List in Java
Let’s convert the set into a list. There are multiple ways of doing so, each with subtle differences.
1. List Constructor with Set Argument
The most straightforward way is by passing the set as an argument while creating the list.
import java.util.*;
public class Main {
public static void main(String[] args)
{
Set<Integer> a = new HashSet<>();
a.add(1);
a.add(2);
a.add(3);
a.add(1);
List<Integer> arr = new ArrayList<>(a);
System.out.println(arr);
System.out.println(arr.get(1));
}
}
Since the set has been converted to a list, the elements are now ordered. This means we can use the get()
method to access elements by index.
2. Using Conventional For Loop
We can use a conventional for loop to explicitly copy the elements from the set to the list.
import java.util.*;
public class Main {
public static void main(String[] args)
{
Set<Integer> a = new HashSet<>();
a.add(1);
a.add(2);
a.add(3);
a.add(1);
List<Integer> arr = new ArrayList<>();
for (int i : a)
arr.add(i);
System.out.println(arr);
System.out.println(arr.get(1));
}
}
The for loop iterates over the set and adds each element to the list.
3. List addAll() Method
The addAll()
method of List
adds multiple values to the list at once. It also works for adding elements of a set to a list.
import java.util.*;
public class Main {
public static void main(String[] args)
{
Set<Integer> a = new HashSet<>();
a.add(1);
a.add(2);
a.add(3);
a.add(1);
List<Integer> arr = new ArrayList<>();
arr.addAll(a);
System.out.println(arr);
System.out.println(arr.get(1));
}
}
4. Stream API collect() Method
The Stream.collect()
method, available from Java 8 onwards, collects all Stream elements into a List
.
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args)
{
Set<Integer> a = new HashSet<>();
a.add(1);
a.add(2);
a.add(3);
a.add(1);
List<Integer> arr = a.stream().collect(Collectors.toList());
System.out.println(arr);
System.out.println(arr.get(1));
}
}
Note that Stream.collect()
does not guarantee the type or thread-safety of the returned List
.
5. List.copyOf() Method
From Java 10 onwards, the copyOf()
method creates an unmodifiable list from a collection.
import java.util.*;
public class Main {
public static void main(String[] args)
{
Set<Integer> a = new HashSet<>();
a.add(1);
a.add(2);
a.add(3);
a.add(1);
List<Integer> arr = List.copyOf(a);
System.out.println(arr);
System.out.println(arr.get(1));
}
}
Conclusion to Convert Set to List
We explored multiple methods to convert a set into a list in Java. Each method has its advantages and limitations. For example, copyOf()
creates an immutable list that doesn’t support null
values, while addAll()
and constructors are reliable for general use cases. Choose the method based on your specific requirements.