Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Stream.max() handle equality?

Although I suspect the answer to be "It's not specified"...

If there are multiple "greatest/lowest" elements in a Stream which the Comparator passed to the max or min methods considers equal (returns 0), is it specified somewhere which element will be found?

like image 730
Hulk Avatar asked Apr 19 '16 07:04

Hulk


People also ask

How does stream Max work?

Stream. max() returns the maximum element of the stream based on the provided Comparator. A Comparator is a comparison function, which imposes a total ordering on some collection of objects. max() is a terminal operation which combines stream elements and returns a summary result.

Why does stream Max return optional?

The Stream max() method will return an Optional that contains the largest value of the stream. The largest values are evaluated based on the passed Comparator argument. If the stream is empty, then an empty Optional is returned. If the largest value returned is null , then NullPointerException is thrown.

How do you find the max element in an array using stream?

2. Using Stream. max() method. The idea is to convert the list into a Stream and call Stream#max() that accepts a Comparator to compare items in the stream against each other to find the maximum element, and returns an Optional containing the maximum element in the stream according to the provided Comparator .

What is reduce method in Java 8?

Java Stream reduction A reduction is a terminal operation that aggregates a stream into a type or a primitive. The Java 8 Stream API contains a set of predefined reduction operations, such as average , sum , min , max , and count , which return one value by combining the elements of a stream.


1 Answers

It’s indeed hard to pull a definite statement from the documentation only. If we try to draw a conclusion from the general description of the “Reduction” process and similar hints of the documentation, it will always feel like we’re possibly doing too much interpretation.

However, there’s an explicit statement regarding this matter from Brian Goetz who’s quite an authority regarding the Stream API:

If the stream is ordered (such as the streams you get from an array or List), it returns the first element that is maximal in the event of multiple maximal elements; only if the stream is unordered is it allowed to pick an arbitrary element.

It’s a pity that such an explicit statement isn’t made right at the documentation of Stream.max, but at least it’s in line with our experience and knowledge of the implementation (those of us who looked at the source code). And not to forget, practical considerations, as it’s easy to say “pick any rather than first” via unordered().max(comparator) with the current state of affairs than saying “pick first rather than any” if max was allowed to pick an arbitrary element in the first place.

like image 94
Holger Avatar answered Sep 26 '22 07:09

Holger