Get last element of Stream/List in a one-liner
It is possible to get the last element with the method Stream::reduce. The following listing contains a minimal example for the general case: Stream<T> stream = …; // sequential or parallel stream Optional<T> last = stream.reduce((first, second) -> second); This implementations works for all ordered streams (including streams created from Lists). For unordered streams it … Read more