Sub topic 8
Sorting, Removing Duplicates, and Limiting
You already know how filter and map choose and change elements. This chapter covers four more intermediate operations. An intermediate operation is a step that takes a stream and returns a new stream. These four change which elements come through, or the order they come in:
sorted()puts elements in order.distinct()removes duplicates.limit(n)keeps only the firstnelements.skip(n)throws away the firstnelements.
Sorting with sorted
sorted() with no arguments uses the natural order of the elements. Natural order is the built-in order for a type: A to Z for strings and smallest to largest for numbers.
To use a different order, pass a Comparator. A comparator is an object that decides which of two elements comes first. Comparator.comparing(String::length) sorts by length. Comparator.reverseOrder() flips the natural order.
Removing duplicates with distinct
distinct() keeps the first copy of each element and drops any later copies. It uses equals to decide whether two elements are the same.
Trimming with limit and skip
limit(3) passes on at most three elements. skip(2) drops the first two and passes on the rest. If you use them together, you get one "page" of results.
A worked example
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortDistinctLimitExample {
public static void main(String[] args) {
List<String> fruits = List.of("pear", "apple", "pear", "fig", "kiwi", "apple");
List<String> unique = fruits.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(unique); // [pear, apple, fig, kiwi]
List<String> alphabetical = fruits.stream()
.distinct()
.sorted()
.collect(Collectors.toList());
System.out.println(alphabetical); // [apple, fig, kiwi, pear]
List<String> page = fruits.stream()
.distinct()
.sorted()
.skip(1)
.limit(2)
.collect(Collectors.toList());
System.out.println(page); // [fig, kiwi]
List<String> byLength = fruits.stream()
.distinct()
.sorted(Comparator.comparing(String::length))
.collect(Collectors.toList());
System.out.println(byLength); // [fig, pear, kiwi, apple]
List<String> reversed = fruits.stream()
.distinct()
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
System.out.println(reversed); // [pear, kiwi, fig, apple]
}
}In byLength, "pear" and "kiwi" both have four letters. sorted keeps them in the order they arrived, so "pear" stays ahead of "kiwi".
Order matters
The position of each step changes the result. If limit(2) comes before sorted(), the stream takes the first two elements of the source and then sorts only those two. It does not give you the two smallest elements.
Exercise
Given List.of(5, 3, 5, 1, 3, 9), what does list.stream().distinct().sorted().limit(2).collect(Collectors.toList()) return?
Show answer
[1, 3]. distinct gives 5, 3, 1, 9. sorted gives 1, 3, 5, 9. limit(2) keeps 1 and 3.
Exercise
Write a pipeline that takes List.of("b", "a", "c", "a") and returns the unique letters in reverse alphabetical order.
Show answer
list.stream().distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList()) returns [c, b, a].