Sorting

The following two intermediate operations sort the stream elements. Naturally, such an operation cannot be finished until all of the elements are emitted, so it creates a lot of overhead, slows down performance, and has to be used either for the small size streams:

Here is a demo code:

List<String> list = List.of("2", "1", "5", "4", "3");
list.stream().sorted().forEach(System.out::print); //prints: 12345
list.stream().sorted(Comparator.reverseOrder())
.forEach(System.out::print); //prints: 54321