This post will discuss how to convert a set to a list in Java.

1. Naive solution

A naive solution is to create an empty list and add every element of the specified set to it.

2. Using Constructor

We can use the ArrayList constructor, which can take another collection object to construct a new list containing all elements of the specified set.

3. Java 8 and above

In Java 8, we can use the Stream to convert a set to a list by converting the specified set to a sequential Stream using Set.stream() and using a Collector to accumulate the input elements into a new List.

 
Since Collectors.toList() doesn’t guarantee on the type of the List returned, we can use Collectors.toCollection() to specify the desired Collection:

4. Using Guava Library

We can also use Guava API to convert a set to a list.

1. Lists.newArrayList() creates a mutable ArrayList instance containing the elements of the specified set. This method is preferred if we need to add or remove elements later, or some of the elements can be null.

 
2. We can also use ImmutableList.copyOf that returns an immutable list containing the specified set elements. This should be preferred when mutability is not needed.

That’s all about converting Set to List in Java.