-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
None
-
generic
-
generic
A DESCRIPTION OF THE PROBLEM :
To get a single element from a stream the methods `findAny` or `findFirst` are usually used.
In alot of scenarios exactly one element is expected to match.
Developers are usually ignoring this and handling this could help preventing bugs due to unexpected elements in the stream.
The suggestion is to add a `Optional<T> findOne()` method to the `Stream` class to cover this case, the internal implementation could call the `reduce(BinaryOperator<T>)` method. The operator should fail with an `IllegalStateException` in case it is called.
The operator should not take care of identical / equal elements, this can be done by calling `stream.distinct().findOne()`.
There are other areas in the Java framework failing in case of ambiguity e.g. Collectors.toMap in case of the same key. So this is a common issue and it might be worth adding a new Exception for this, e.g. `AmbigouseException extends IllegalStateException`.
To get a single element from a stream the methods `findAny` or `findFirst` are usually used.
In alot of scenarios exactly one element is expected to match.
Developers are usually ignoring this and handling this could help preventing bugs due to unexpected elements in the stream.
The suggestion is to add a `Optional<T> findOne()` method to the `Stream` class to cover this case, the internal implementation could call the `reduce(BinaryOperator<T>)` method. The operator should fail with an `IllegalStateException` in case it is called.
The operator should not take care of identical / equal elements, this can be done by calling `stream.distinct().findOne()`.
There are other areas in the Java framework failing in case of ambiguity e.g. Collectors.toMap in case of the same key. So this is a common issue and it might be worth adding a new Exception for this, e.g. `AmbigouseException extends IllegalStateException`.