A set in Java is an unordered collection of unique/distinct elements while a List is an ordered collection of elements that can store duplicate entries. Converting a set into a list is a common task that Java users perform because of several reasons. For example, a set-to-list conversion allows a user to remove duplicate elements, sort unordered data, access elements using indexes, and much more.
Considering the above-mentioned significance, this post will discuss several built-in and third-party methods to convert a set into a list in Java. But before that, let’s explore some notable distinctions between the set and the list.
Set Vs. List – What’s the Difference
The below table illustrates several differences between the set and the list data structures with respect to different parameters:
Set | List |
---|---|
It keeps only unique elements/entries. | It allows duplicate entries/elements. |
Elements are not kept in order. | It maintains the insertion order of the elements/entries. |
It can have only one null entry. | It can have more than one null entry. |
Its elements can’t be accessed with its index/position. | Elements can be accessed using index number or position. |
It is implemented using HashSet or LinkedHashSet. | It is implemented via ArrayList, Stack, LinkedList, or Vector. |
It is traversed through the iterator() method. | It is traversed through the listIterator() method. |
Now let’s head towards the next section where you will learn different methods of converting a set into a list in Java.
How to Convert a Set to a List in Java
You can convert a Java set to a list using
- Method 1: List.addAll()
- Method 2: List Constructor
- Method 3: Java 8 Stream API
- Method 4: copyOf()
- Method 5: Set Traversing
- Method 6: Guava Library
- Method 7: Apache Commons
Let’s start with the addAll() method.
Method 1: Converting a Set to a List Using List.addAll()
The most convenient way of converting a set to a List is using the “List.addAll()” method. This method adds/appends all elements of the given collection to a list, as explained below:
package javaBeat;
import java.util.*;
public class SetToList {
public static void main(String[] args) {
//set creation
Set<String> stdSet = new HashSet<String>();
stdSet.add("Joseph");
stdSet.add("John");
stdSet.add("Mike");
stdSet.add("Alex");
stdSet.add("Ambrose");
System.out.println("The Given Set is ==> " + stdSet);
List<String> stdList = new ArrayList<String>();
stdList.addAll(stdSet);
System.out.print("The Converted List is ==> " + stdList);
}
}
In this example, we create a string-type set “stdSet” using the HashSet constructor. We initialize this set with different strings using the add() method. After this, we create a new list using the ArrayList constructor. Next, we invoke the addAll() method of the ArrayList class to append the set elements to the “stdList”. Finally, we print the original set and the converted list on the console:
Method 2: Converting a Set to a List Using List Constructor
You can create a new list and specify the given set as an argument to the list constructor. This way, the given set will be converted into a list, as shown in the below code:
import java.util.*;
public class SetToList {
public static void main(String[] args) {
Set<Integer> stdSet = new HashSet<Integer>();
stdSet.add(100);
stdSet.add(110);
stdSet.add(-50);
stdSet.add(20);
stdSet.add(120);
System.out.println("The Given Set is ==> " + stdSet);
List<Integer> stdList = new LinkedList<Integer>(stdSet);
System.out.print("The Converted List is ==> " + stdList);
}
}
We create an integer-type set “stdSet” and initialize it with different integers using the add() method. After this, we create a new list using LinkedList implementation and pass the “stdSet” as its argument. This will assign all the elements of the stdSet to the stdList, as shown below:
Method 3: Converting a Set to List Using Java 8 Stream API
You can use the “set.stream()” method of the stream API to convert the given set into a stream. After this, use the collect() method to collect the converted stream into a new list, as illustrated in the following code:
import java.util.*;
import java.util.stream.*;
public class SetToList {
public static <T> List<T> SetToListConversion(Set<T> givenSet) {
return givenSet.stream().collect(Collectors.toList());
}
public static void main(String args[]) {
Set<String> stdSet = new HashSet<String>();
stdSet.add("Joseph");
stdSet.add("John");
stdSet.add("Mike");
stdSet.add("Alex");
stdSet.add("Ambrose");
System.out.println("The Given Set is ==> " + stdSet);
List<String> stdList = SetToListConversion(stdSet);
System.out.println("The Converted List is ==> " + stdList);
}
}
In this code, we create a generic function to convert the given set into a list and return it. In this function, we use different methods of Java 8 Stream API to perform the set-to-list conversion. From the main() method, we invoke this function and pass the given set as its argument. The function will return a list that is stored in a list-variable “stdList”. Finally, we print the retrieved list on the console:
Method 4: Converting a Set to List Using copyOf()
The copyOf() is a static method of the “java.util.List” interface that retrieves an unmodifiable clone/copy of the given collection. You can use this method in Java to convert the given set into a list, as demonstrated in the following code:
import java.util.*;
public class SetToList {
public static void main(String args[]) {
Set<String> stdSet = new HashSet<String>();
stdSet.add("Joseph");
stdSet.add("John");
stdSet.add("Mike");
stdSet.add("Alex");
stdSet.add("Ambrose");
System.out.println("The Given Set is ==> " + stdSet);
List<String> stdList = new ArrayList<>();
stdList = List.copyOf(stdSet);
System.out.println("The Converted List is ==> " + stdList);
}
}
In this code, we create a set, initialize it with some strings, and print it on the console. After this, we create an empty ArrayList of type String. Next, we invoke the “List.copyOf()” method on the given set and store the retrieved list in the list “stdList”. Finally, we print the given set and the converted list on the console:
Method 5: Converting a Set to a List Using Set Traversing
You can convert a set to a list by traversing the given set. For this, first, create a new list equal to the length of the given set and then traverse a set using a loop. Within the loop, use the add() method to add/insert each element of the given set into the newly created list:
import java.util.*;
public class SetToList {
public static void main(String args[]) {
Set<Integer> empSet = new HashSet<Integer>();
empSet.add(001);
empSet.add(002);
empSet.add(003);
empSet.add(004);
System.out.println("The Original Set is ==> " + empSet);
int size = empSet.size();
List<Integer> empList = new ArrayList<Integer>(size);
for (Integer list : empSet)
empList.add(list);
System.out.println("The Converted List is ==> " + empList);
}
}
Finally, print the created list on the console, as follows:
Method 6: Converting a Set to a List Using Guava Library
The “Guava” is a third-party library that offers several extremely useful utilities. To access this library, first, add the below-stated dependency in the “pom.xml” file of your Maven project:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.3-jre</version>
</dependency>
Once the respective dependency is added to your maven project, use the following import statements to access and use any of its methods:
import java.util.*;
import com.google.common.collect.Lists;
public class SetToListJava {
public static void main(String args[]) {
Set<Integer> empSet = new HashSet<Integer>();
empSet.add(100);
empSet.add(400);
empSet.add(200);
empSet.add(300);
System.out.println("The Given Set is ==> " + empSet);
List<Integer> empList = Lists.newArrayList(empSet);
System.out.println("The Converted List is ==> " + empList);
}
}
In this code, we use the “Lists.newArrayList()” method to create a mutable list from the given set “empSet”:
Method 7: Converting a Set to a List Using Apache Commons
Apache Commons is a third-party library that is freely accessible in Java. To use this library, you need to create a Maven/Gradle project and include the corresponding dependency in the “pom.xml” or “build.gradle” file. For instance, we include the below-given “Apache Commons” dependency in our “pom.xml” file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
Next, we add the required packages in our Java code using the import statement:
import java.util.*;
import org.apache.commons.collections4.CollectionUtils;
public class SetToListJava {
public static void main(String args[]) {
Set<String> empSet = new HashSet<String>();
empSet.add("Anees");
empSet.add("Asghar");
empSet.add("John");
empSet.add("Joseph");
System.out.println("The Given Set is ==> " + empSet);
List<String> empList = new ArrayList<>(4);
CollectionUtils.addAll(empList, empSet);
System.out.println("The Converted List is ==> " + empList);
}
}
In the main() method, we create a string-type set and initialize it with some values using the add() method. Next, we create an ArrayList of size four. After this, we use the CollectionUtils.addAll() method to assign all elements of the empSet to the empList. Finally, we print the created list on the console:
How to Convert a List to a Set in Java
There are several ways of converting a list into a set in Java, such as using a set constructor, the addAll() method, Java streams, etc.
Example: List to Set Conversion Using addAll()
In the below-stated code, we use the addAll() method in the following code to convert a list into a set:
import java.util.*;
public class ListToSetJava {
public static void main(String[] args) {
List<String> empList = new ArrayList<>(Arrays.asList("Anees", "Asghar", "Javabeat", ".net"));
System.out.println("The Given List is ==> " + empList);
Set<String> empSet = new HashSet<>();
empSet.addAll(empList);
System.out.println("The Converted Set is ==> " + empSet);
}
}
We create a string-type list “empList”, initialize it with some values, and print it on the console. After this, we create a set “empSet” using HashSet implementation. We invoke the addAll() method on the empSet to initialize it with the empList. Finally, we print the converted/created set on the console:
This is how you can convert a set to a list or a list back to a set in Java.
Conclusion
To convert a Set to a List in Java, you can use different plain Java methods, such as “List.addAll()”, “List Constructor”, “Java 8 Stream API”, “copyOf()”, and “Set Traversing”. Also, you can use different utility methods of third-party libraries, like “Guava” and “Apache Commons”. Among all the mentioned approaches, the “addAll()” and “List constructor” are most commonly used.