Combining distinct and sorted in Pipelines
What you'll learn: How to optimize stream pipelines by strategically ordering distinct() before sorted() to improve performance.
Understanding the Optimization
When you need both unique elements and a sorted result from a stream, the order in which you apply these operations matters significantly. Since both distinct() and sorted() are stateful operations, placing distinct() before sorted() reduces the workload for sorting.
Think of it like organizing a messy drawer: it's much easier to sort 10 unique items than to sort 100 items and then remove duplicates. The sorting algorithm has fewer elements to work with.
Why distinct() Should Come First
When you call distinct() first, you eliminate duplicates early in the pipeline. This means sorted() receives a smaller collection to sort, which can dramatically improve performance—especially with large datasets containing many duplicates.
// ✅ Optimal: distinct before sorted
stream.distinct()
.sorted()
.collect(Collectors.toList());
// ❌ Less efficient: sorted before distinct
stream.sorted()
.distinct()
.collect(Collectors.toList());
In the first example, if your original stream has 1,000 elements but only 200 unique values, sorted() processes just 200 elements. In the second example, sorted() unnecessarily processes all 1,000 elements before distinct() removes the duplicates.
The Result Is the Same
Both orderings produce identical results—a sorted list of unique elements. The difference is purely performance. The sorting algorithm's complexity (typically O(n log n)) benefits significantly from a smaller n.
Key Takeaway:
Always apply distinct() before sorted() in stream pipelines to minimize the number of elements the sorting operation must process.