Java 8 Streams Java Java API
java.util.stream.LongStream
OptionalLong max()
This terminal operation returns an OptionalLong describing the maximum element of this stream, or an empty optional if this stream is empty. This is a special case of a reduction.
OptionalLong
package com.logicbig.example.longstream;import java.util.OptionalLong;import java.util.stream.LongStream;public class MaxExample { public static void main(String... args) { LongStream stream = LongStream.of(4, 7, 9, 11, 13, 17); OptionalLong max = stream.max(); long l = max.orElseGet(() -> -1); System.out.println(l); }}
17