Why is the sorted() method on Stream not named sort()?
list.stream().sort()
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityWhy is the sorted() method on Stream not named sort()?
list.stream().sort()
According to the documentation, the sorted()
method is a stateful intermediate operation.
You can read more about what that means here:
Intermediate operations return a new stream. They are always lazy; executing an intermediate operation such as
filter()
does not actually perform any filtering, but instead creates a new stream that, when traversed, contains the elements of the initial stream that match the given predicate.
So sorted()
describes the new stream created by the sorted() method, not the act of sorting.
sort
method in the JRE, and that sort
method sorts in-place. Having a lazy, non-destructive method also called sort
would be confusing.
Sep 6, 2017 at 5:57