Examples of Conversion Between List and Set in Java

This tutorial covers examples of converting a Java List to a Set and a Set to a List using Plain Java, Guava Library and Apache Commons Collections Library.

Converting a Set to a List

As the Sets are a unique collection of elements and lists are not, we can easily convert Sets to Lists.

Set to List using Plain Java

The most straightforward way of converting a Java Set to a List is to use the ArrayList constructor and supply the source Set. The ArrayList constructor creates a new ArrayList containing all the elements from the given Java collection.
Example of converting a Java Set to a mutable List using Java.

Set<String> set = Set.of("a", "b", "c"); List<String> list = new ArrayList<>(set);
Code language: Java (java)

The List we get is a mutable List. However, if we want to create an immutable List, we can use the copyOf() method of the List interface.

Example of converting a Java Set to an immutable List using Java.

Set<String> set = Set.of("a", "b", "c"); List<String> list = List.copyOf(set);
Code language: Java (java)

Set to List Using Guava Library

Next, we will see how to convert a Java Set to a List using the Guava Library. The Lists class of the library provides the newArrayList(), a factory method to create a new ArrayList containing all the elements from the given Java Collection.

Examples of converting a Java Set to a List using Guava.

Set<String> set = Set.of("a", "b", "c"); List<String> list = Lists.newArrayList(set);
Code language: Java (java)

Set to List Using Apache Commons Library

Similarly, we can use Apache Commons Collections Library to convert a Java Set to a List. We can create an empty ArrayList and then use the addAll() method of the CollectionUtils class to copy all the elements from the Set in the ArrayList.

Set<String> set = Set.of("a", "b", "c"); List<String> list = new ArrayList<>(); CollectionUtils.addAll(list, set);
Code language: Java (java)

Converting a List to a Set

We understood how we could convert a Java Set to a List. Now, let’s learn to convert a Java List to a Set. Please remember that a List is not a collection of unique elements. Thus, all the duplicate elements are removed when we convert a List to a Set.

List to Set Using Plain Java

The most basic solution to convert a Java List to a Set is to use the HashSet constructor and provide the List. The constructor creates a new HashSet instance containing all the unique elements of the given List.

Example of converting a Java List to a mutable HashSet using Java.

List<String> list = List.of("a", "b", "c", "a"); Set<String> set = new HashSet<>(list);
Code language: Java (java)

The HashSet created by the constructor is a mutable one. To create an immutable Set from a List, we can use the copyOf() method of the Set interface.

List<String> list = List.of("a", "b", "c", "a"); Set<String> set = Set.copyOf(list);
Code language: Java (java)

List to Set Using Guava Library

The Sets class of the Guava Library provides the newHashSet() method that creates a new HashSet containing all the elements from the given Java Collection.

List<String> list = List.of("a", "b", "c", "a"); Set<String> set = Sets.newHashSet(list);
Code language: Java (java)

List to Set Using Apache Commons Library

We can also use Apache Commons Collections Library to convert a Java List to a Set. To do so, we create an empty HashSet and use the addAll() method of CollectionUtils class to copy all the elements from the List into the Set.

List<String> list = List.of("a", "b", "c", "a"); Set<String> set = new HashSet<>(); CollectionUtils.addAll(set, list);
Code language: Java (java)

Summary

This tutorial demonstrated ways of Converting a Java List to a Set and Converting a Java Set to a List using plain Java, Google Guava Library, and Apache Commons Collections Library.

Please refer to our GitHub Repository for the complete source code of the examples.