Convert Set to List in Java
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.
Java Program
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
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.
- 1
namesis built as aLinkedHashSet<String>, which — unlike a plain HashSet — iterates its elements in the exact order they were originally inserted. - 2
new ArrayList<>(names)uses the constructor overload that accepts anyCollection, copying each element from the Set into a new, indexable List. - 3Because the source Set is a
LinkedHashSet, the resulting List's order exactly matches the insertion order —Maya, thenLiam, thenNoah. - 4A plain
HashSetwould still convert the same way, but its own iteration order isn't guaranteed to match insertion order at all.
Maya, Liam, and Noah into the LinkedHashSet in that order means new ArrayList<>(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.
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
Approach 2: Java 8
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
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.
- 1
names.stream()opens a stream over the Set's elements, in whatever order that Set iterates in — here, the LinkedHashSet's insertion order. - 2
.collect(Collectors.toList())gathers the streamed elements into a new List, in that same order. - 3The result is equivalent to the constructor version — a fresh, independent List holding the Set's elements.
- 4This form is most useful when the conversion is one step in a larger pipeline, rather than a standalone conversion on its own.
{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.
Why: Streaming and collecting still visits each of the Set's n elements exactly once, the same cost as the constructor form.