This tutorial covers examples to Convert a List to a Set as well as a Set to a List using Plain Java, Guava Library and Apache Commons Collections Library.
Tutorial Contents
Converting Set to List
As the Sets are a unique collection of elements and lists are not, we can easily convert Sets to Lists.
Using Plain Java
We can use ArrayList
constructor by passing a Set instance to create a list.
Set<String> stringSet = Set.of("a", "b", "c");
List<String> stringList = new ArrayList<>(stringSet);
Code language: Java (java)
Also, we can use copyOf
method on List interface to create an Unmodifiable List.
Set<String> stringSet = Set.of("a", "b", "c");
List<String> stringList = List.copyOf(stringSet);
Code language: Java (java)
Using Guava Library
Next, we will see how to do the same using Guava Library. We can use the utility method Lists#newArrayList
provided by the library.
Set<String> stringSet = Set.of("a", "b", "c");
List<String> stringList = Lists.newArrayList(stringSet);
Code language: Java (java)
Using Apache Commons Collections Library
Similarly, we can use Apache Commons Collections Library. To do that, we need to create an empty list and copy all the elements from Set using CollectionsUtils
class.
Set<String> stringSet = Set.of("a", "b", "c");
List<String> stringList = new ArrayList<>();
CollectionUtils.addAll(stringList, stringSet);
Code language: Java (java)
Converting List to Set
We have seen examples of Converting Set to List. Now, in this section we will cover examples of converting a List into Set.
Remember, list is not a collection of unique elements. Thus, when we convert a List to a Set all the duplicate elements are removed.
Using Plain Java
The most basic solution to convert a Set to List is to use HashSet
constructor and pass reference to the list instance.
List<String> stringList = List.of("a", "b", "c", "a");
Set<String> stringSet = new HashSet<>(stringList);
Code language: Java (java)
Also, we can use copyOf
method provided by the Set interface. Remember that, the set instance we get this way, is unmodifiable.
List<String> stringList = List.of("a", "b", "c", "a");
Set<String> stringSet = Set.copyOf(stringList);
Code language: Java (java)
Using Guava Library
The Sets
class provided by Guava Library provides a newHashSet
method which accepts an instance of Iterable. Next, example shows using Guava.
List<String> stringList = List.of("a", "b", "c", "a");
Set<String> stringSet = Sets.newHashSet(stringList);
Code language: Java (java)
Using Apache Commons Collections Library
Finally, we will see an example of using Apache Commons Collections Library. We need to first make and empty ArrayList
, and then use the CollectionUtils#addAll
method to copy list elements.
List<String> stringList = List.of("a", "b", "c", "a");
Set<String> stringSet = new HashSet<>();
CollectionUtils.addAll(stringList);
Code language: Java (java)
Summary
In this quick tutorial we covered various ways of Converting a List to a Set and Converting a Set to a List in Java. We used Plain Java, Guava Library and Apache Commons Collections Library to do the conversions. For more on Java, please visit Java Tutorials.