Sort Set in Java
Problem
A HashSet doesn't guarantee any particular iteration order, so sorting its contents means copying them into a List first and sorting that.
Given a Set of names in no particular order, produce a List of the same names sorted alphabetically.
Java Program
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class SortSet {
public static void main(String[] args) {
Set<String> names = Set.of("Charlie", "Alice", "Bob");
List<String> sortedNames = new ArrayList<>(names);
Collections.sort(sortedNames); // Set has no order of its own — sort the copy instead
System.out.println(sortedNames);
}
}Output
Core Logic
Copying the Set's elements into an ArrayList first, then sorting that List in place, works around the fact that a Set itself has no sort() method of its own.
- 1
Set<String> names = Set.of(...)creates a Set whose iteration order isn't specified at all — it could print in any order. - 2
new ArrayList<>(names)copies every element into a List, which — unlike a Set — does have a defined, sortable order. - 3
Collections.sort(sortedNames)sorts that List in place, using String's natural (alphabetical) ordering. - 4Only the List is ever printed — the original Set's own iteration order never has to be observed.
Charlie, Alice, and Bob in some unspecified order; copying them into a List and sorting it always produces [Alice, Bob, Charlie], regardless of what order the Set itself happened to iterate in.Key Point: A TreeSet would keep its elements sorted automatically from the start — this convert-then-sort technique is for the more general case of sorting a collection type, like HashSet, that was never ordered to begin with.
Why: Copying the Set's n elements into a List costs O(n), and sorting that List costs O(n log n), which dominates the total.
Key Concepts
Approach 2: Java 8
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class SortSetStream {
public static void main(String[] args) {
Set<String> names = Set.of("Charlie", "Alice", "Bob");
// Sorts the streamed elements using natural ordering, collecting into a new List
List<String> sortedNames = names.stream()
.sorted()
.collect(Collectors.toList());
System.out.println(sortedNames);
}
}
Output
Core Logic
Streaming the Set and sorting it directly produces the same sorted List as the copy-then-sort technique, in a single chained call.
- 1
names.stream()opens a stream over the Set's elements, in whatever unspecified order the Set itself iterates in. - 2
.sorted()sorts the streamed elements using their natural ordering — alphabetical, for Strings. - 3
.collect(Collectors.toList())gathers the sorted elements into a new List. - 4The original Set is never modified — like the primary approach, only the newly-produced List reflects the sorted order.
{Charlie, Alice, Bob} collects into [Alice, Bob, Charlie], the same result the copy-then-sort version produces.Key Point: sorted() replaces the explicit copy-into-ArrayList-then-Collections.sort() sequence with a single chained call — the underlying idea (a Set has no order of its own, so build a sorted List instead) is unchanged.
Why: sorted() still needs to buffer and sort all n elements internally, and collect() builds a new list to hold them — the same overall cost as copying into an ArrayList and calling Collections.sort().