Stream findFirst() in Java with examples Last Updated : 06 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report Stream findFirst() returns an Optional (a container object which may or may not contain a non-null value) describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned. Syntax : Optional<T> findFirst() Where, Optional is a container object which may or may not contain a non-null value and T is the type of objects and the function returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. Exception : If the element selected is null, NullPointerException is thrown. Note : findAny() is a terminal-short-circuiting operation of Stream interface. This method returns first element satisfying the intermediate operations. Example 1 : findFirst() function on Stream of Integers. Java // Java code for Stream findFirst() // which returns an Optional describing // the first element of this stream, or // an empty Optional if the stream is empty. import java.util.*; class GFG { // Driver code public static void main(String[] args) { // Creating a List of Integers List<Integer> list = Arrays.asList(3, 5, 7, 9, 11); // Using Stream findFirst() Optional<Integer> answer = list.stream().findFirst(); // if the stream is empty, an empty // Optional is returned. if (answer.isPresent()) { System.out.println(answer.get()); } else { System.out.println("no value"); } } } Output : 3 Example 2 : findFirst() function on Stream of Strings. Java // Java code for Stream findFirst() // which returns an Optional describing // the first element of this stream, or // an empty Optional if the stream is empty. import java.util.*; class GFG { // Driver code public static void main(String[] args) { // Creating a List of Strings List<String> list = Arrays.asList("GeeksforGeeks", "for", "GeeksQuiz", "GFG"); // Using Stream findFirst() Optional<String> answer = list.stream().findFirst(); // if the stream is empty, an empty // Optional is returned. if (answer.isPresent()) { System.out.println(answer.get()); } else { System.out.println("no value"); } } } Output : GeeksforGeeks Comment More infoAdvertise with us Next Article Stream flatMapToInt() in Java with examples S Sahil_Bansall Follow Improve Article Tags : Misc Java Technical Scripter Java - util package Java-Functions java-stream Java-Stream interface +3 More Practice Tags : JavaMisc Similar Reads Stream filter() in Java with examples Stream filter(Predicate predicate) returns a stream consisting of the elements of this stream that match the given predicate. This is an intermediate operation. These operations are always lazy i.e, executing an intermediate operation such as filter() does not actually perform any filtering, but ins 3 min read Java Stream findAny() with examples Stream findAny() returns an Optional (a container object which may or may not contain a non-null value) describing some element of the stream, or an empty Optional if the stream is empty. Syntax of findAny()Optional<T> findAny() Parameters1. Optional is a container object which may or may not 4 min read Stream flatMap() in Java with examples Stream flatMap(Function mapper) returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Stream flatMap(Function mapper) is an intermediate operation. These operations 5 min read Stream flatMapToInt() in Java with examples Stream flatMapToInt(Function mapper) returns an IntStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Stream flatMapToInt(Function mapper) is an intermediate operation. The 2 min read Stream empty() in Java with Examples Stream empty() creates an empty sequential Stream. Syntax : static <T> Stream<T> empty() Parameters : T : The type of stream elements. Stream : A sequence of objects that supports various methods which can be pipelined to produce the desired result. Return Value : Stream empty() returns 1 min read Stream empty() in Java with Examples Stream empty() creates an empty sequential Stream. Syntax : static <T> Stream<T> empty() Parameters : T : The type of stream elements. Stream : A sequence of objects that supports various methods which can be pipelined to produce the desired result. Return Value : Stream empty() returns 1 min read Like