stream.sorted() / distinct() / limit() / skip()
As intermediate stream operations, use sorted() to sort elements, distinct() to remove duplicates, limit() to cap the number of elements, and skip() to skip leading elements (Java 8+). These can be combined with method chaining.
Syntax
// Sorts elements in natural order (ascending). stream.sorted(); // Sorts elements using a comparator. stream.sorted(Comparator.reverseOrder()); // Removes duplicate elements. stream.distinct(); // Keeps only the specified number of elements from the start. stream.limit(count); // Skips the specified number of elements from the start. stream.skip(count); // Passes each element through while performing an action (for debugging). stream.peek(element -> action);
Method List
| Method | Description |
|---|---|
| sorted() | An intermediate operation that sorts elements in natural order (ascending). Elements must implement Comparable. |
| sorted(Comparator) | An intermediate operation that sorts elements using the specified comparator. |
| distinct() | An intermediate operation that removes duplicate elements. Duplicates are determined using equals() and hashCode(). |
| limit(long maxSize) | An intermediate operation that keeps only the specified number of elements from the start of the stream. |
| skip(long n) | An intermediate operation that skips the specified number of elements from the start of the stream. |
| peek(Consumer) | An intermediate operation that passes elements through while executing an action on each. Primarily used for debugging. |
Sample Code
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
List<Integer> numbers = Arrays.asList(5, 3, 1, 4, 2, 3, 5, 1);
// Sorts in ascending order.
numbers.stream()
.sorted()
.forEach(System.out::println); // Prints: 1, 1, 2, 3, 3, 4, 5, 5.
// Sorts in descending order.
numbers.stream()
.sorted(Comparator.reverseOrder())
.forEach(System.out::println); // Prints in descending order.
// Removes duplicates.
numbers.stream()
.distinct()
.forEach(System.out::println); // Prints: 5, 3, 1, 4, 2 (after removing duplicates).
// Keeps only the first 3 elements.
numbers.stream()
.sorted()
.limit(3)
.forEach(System.out::println); // Prints: 1, 1, 2.
// Skips the first 3 elements.
numbers.stream()
.sorted()
.skip(3)
.forEach(System.out::println); // Prints the 4th element onward.
// Sorts strings by length.
List<String> words = Arrays.asList("banana", "apple", "kiwi", "cherry");
words.stream()
.sorted(Comparator.comparingInt(String::length))
.forEach(System.out::println); // Prints: kiwi, apple, banana, cherry.
Notes
sorted() is an intermediate operation and does not modify the original collection. Elements must implement Comparable to be sorted (String, Integer, and similar types already do). To sort instances of a custom class, provide a Comparator.
Combining limit() and skip() lets you implement pagination (for example, skip(20).limit(10) retrieves elements 21–30). Because sorted() sorts all elements in the stream, be mindful of performance when working with large datasets.
For stream creation and filtering, see stream() / filter() / map().
If you find any errors or copyright issues, please contact us.