IntStream findFirst() example
Description
IntStream findFirst()
returns an OptionalInt
describing the first element of this stream, or an empty OptionalInt if the stream is empty.
Syntax
findFirst
has the following syntax.
OptionalInt findFirst()
Example
The following example shows how to use findFirst
.
import java.util.OptionalInt;
import java.util.stream.IntStream;
/* w w w . j a v a2 s . c om*/
public class Main {
public static void main(String[] args) {
IntStream i = IntStream.of(1, 2, 3, 4);
OptionalInt n = i.findFirst();
if(n.isPresent()){
System.out.println(n.getAsInt());
}else{
System.out.println("noValue");
}
}
}
The code above generates the following result.