0
0
JavaHow-ToBeginner · 2 min read

Java How to Convert Set to List with Example

You can convert a Set to a List in Java by creating a new ArrayList and passing the set to its constructor like this: List<Type> list = new ArrayList<>(set);.
📋

Examples

InputSet<Integer> set = Set.of(1, 2, 3);
OutputList<Integer> list = [1, 2, 3]
InputSet<String> set = new HashSet<>(); set.add("apple"); set.add("banana");
OutputList<String> list = ["apple", "banana"]
InputSet<String> set = new HashSet<>(); // empty set
OutputList<String> list = []
🧠

How to Think About It

To convert a set to a list, think of the set as a collection of unique items. You want to create a list that contains all these items in order. Since a list can be created from any collection, you just pass the set to the list's constructor, which copies all elements into a new list.
📐

Algorithm

1
Get the input set.
2
Create a new list by passing the set to the list constructor.
3
Return or use the new list.
💻

Code

java
import java.util.*;

public class SetToList {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("apple");
        set.add("banana");
        set.add("cherry");

        List<String> list = new ArrayList<>(set);
        System.out.println(list);
    }
}
Output
[banana, cherry, apple]
🔍

Dry Run

Let's trace converting a set {"apple", "banana", "cherry"} to a list.

1

Create Set

Set contains: [apple, banana, cherry]

2

Convert to List

List created from set: [banana, cherry, apple]

3

Print List

Output: [banana, cherry, apple]

StepSet ContentsList Contents
1[apple, banana, cherry][]
2[apple, banana, cherry][banana, cherry, apple]
3[apple, banana, cherry][banana, cherry, apple]
💡

Why This Works

Step 1: Set to List Conversion

The ArrayList constructor accepts any collection, so passing the set copies all its elements into a new list.

Step 2: Order in List

Since sets do not guarantee order, the list order depends on the set implementation, but all elements are included.

Step 3: Result Usage

The new list can be used like any list, supporting duplicates and indexed access.

🔄

Alternative Approaches

Using addAll() method
java
import java.util.*;

public class SetToListAddAll {
    public static void main(String[] args) {
        Set<Integer> set = Set.of(1, 2, 3);
        List<Integer> list = new ArrayList<>();
        list.addAll(set);
        System.out.println(list);
    }
}
This method creates an empty list first and then adds all set elements. It is slightly longer but clear.
Using Java Streams
java
import java.util.*;
import java.util.stream.*;

public class SetToListStream {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>(Arrays.asList("a", "b", "c"));
        List<String> list = set.stream().collect(Collectors.toList());
        System.out.println(list);
    }
}
Streams provide a flexible way to convert and process collections but add complexity for simple conversion.

Complexity: O(n) time, O(n) space

Time Complexity

Copying all elements from the set to the list requires iterating over each element once, so it is O(n).

Space Complexity

A new list is created to hold all elements, so extra space proportional to the number of elements is needed, O(n).

Which Approach is Fastest?

Using the ArrayList constructor is the fastest and simplest. Using addAll() is similar but slightly longer. Streams add overhead and are best for complex processing.

ApproachTimeSpaceBest For
ArrayList constructorO(n)O(n)Simple and fast conversion
addAll() methodO(n)O(n)Clear step-by-step addition
StreamsO(n)O(n)When further processing is needed
💡
Use new ArrayList<>(set) for a quick and simple conversion from set to list.
⚠️
Trying to cast a set directly to a list without creating a new list causes a ClassCastException.