![]() |
VOOZH | about |
Stream.sorted() is an intermediate, stateful operation that returns a stream with elements sorted in natural order or by a given Comparator. It is stable for ordered streams, requires elements to be Comparable (or a comparator), and may throw ClassCastException if elements are not comparable.
Example:
-18 -9 0 4 25
Explanation:
Stream<T> sorted()
Example 1: This code demonstrates how Stream.sorted() sorts elements of a stream in natural order without modifying the original collection.
Geeks Geeks Welcome You for
Explanation:
Example 2: This code demonstrates sorting a stream of Point objects by their x coordinate using Stream.sorted() with a custom comparator.
1, 100 5, 10 10, 20 50, 2000
Explanation: