1. 程式人生 > >Most efficient way to get the last element of a stream

Most efficient way to get the last element of a stream

val lang ted reduce class ret return imp pretty

Do a reduction that simply returns the current value:

Stream<T> stream; T last = stream.reduce((a, b) -> b).orElse(null);

Most efficient way to get the last element of a stream