Java ProgramsCollectionsConvert Set to List

Convert Set to List in Java

beginner·  Collections  ·  Set

Problem

An ArrayList's constructor also accepts any Collection, so converting a Set into a List is just as direct as the reverse — the resulting List's order simply follows whatever iteration order the source Set produces.

Given a Set of strings, convert it into a List, preserving the order the Set iterates in.

Input
LinkedHashSet with Maya, Liam, Noah added in that order
Output
List: [Maya, Liam, Noah]

Java Program

Java
import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; public class ConvertSetToList { public static void main(String[] args) { Set<String> names = new LinkedHashSet<>(); names.add("Maya"); names.add("Liam"); names.add("Noah"); List<String> nameList = new ArrayList<>(names); // copies elements in the Set's own iteration order System.out.println("List: " + nameList); } }

Output

List: [Maya, Liam, Noah]

Core Logic

Passing the Set directly to ArrayList's copy constructor pulls every element out in whatever order the Set itself iterates in, building a new List from them.

How It Works
  1. 1names is built as a LinkedHashSet&lt;String&gt;, which — unlike a plain HashSet — iterates its elements in the exact order they were originally inserted.
  2. 2new ArrayList&lt;&gt;(names) uses the constructor overload that accepts any Collection, copying each element from the Set into a new, indexable List.
  3. 3Because the source Set is a LinkedHashSet, the resulting List's order exactly matches the insertion order — Maya, then Liam, then Noah.
  4. 4A plain HashSet would still convert the same way, but its own iteration order isn't guaranteed to match insertion order at all.
Inserting Maya, Liam, and Noah into the LinkedHashSet in that order means new ArrayList&lt;&gt;(names) produces exactly [Maya, Liam, Noah].
💡

Key Point: The resulting List's order is entirely inherited from the source Set's own iteration order — a HashSet's unspecified order, a LinkedHashSet's insertion order, or a TreeSet's sorted order would each produce a differently-ordered List from the same elements.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Every element of the source Set is copied into the new ArrayList exactly once, and the resulting List holds all n elements.

Key Concepts

ArrayList constructorSet to List conversionLinkedHashSet iteration order

Approach 2: Java 8

Java
import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class ConvertSetToListStream { public static void main(String[] args) { Set<String> names = new LinkedHashSet<>(); names.add("Maya"); names.add("Liam"); names.add("Noah"); // Streams the Set's elements in its own iteration order, collecting into a new List List<String> nameList = names.stream().collect(Collectors.toList()); System.out.println("List: " + nameList); } }

Output

List: [Maya, Liam, Noah]

Core Logic

Streaming the Set and collecting it into a List expresses the same conversion as a pipeline, which reads naturally when the Set is already the result of an earlier stream operation.

How It Works
  1. 1names.stream() opens a stream over the Set's elements, in whatever order that Set iterates in — here, the LinkedHashSet's insertion order.
  2. 2.collect(Collectors.toList()) gathers the streamed elements into a new List, in that same order.
  3. 3The result is equivalent to the constructor version — a fresh, independent List holding the Set's elements.
  4. 4This form is most useful when the conversion is one step in a larger pipeline, rather than a standalone conversion on its own.
Streaming {Maya, Liam, Noah} (a LinkedHashSet) and collecting it produces [Maya, Liam, Noah], the same result the constructor form gives.
💡

Key Point: For a standalone conversion like this one, new ArrayList<>(names) is shorter and equally clear — the stream form earns its keep once filtering, mapping, or sorting joins the same pipeline.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Streaming and collecting still visits each of the Set's n elements exactly once, the same cost as the constructor form.

Key Concepts

StreamCollectors.toList()

Related Programs