Java Stream findFirst()
May 08, 2024
The findFirst
method of Stream
finds the first element as Optional
in this stream. If stream has no element, findFirst
returns empty Optional
. If the stream has no encounter order then findFirst
may select any element. If the selected element by findFirst
is null, it throws NullPointerException
.
1. Method Syntax
Find thefindFirst
declaration from Java doc.
Optional<T> findFirst()
findFirst
method returns Optional
containing first element in the stream.
The
findFirst
throws NullPointerException
if selects null value.
The
findFirst
is a short-circuiting terminal operation. Stream operations are the group of intermediate and terminal operation. An intermediate operation is a short-circuiting if it may produce a finite stream for infinite input.
2. Examples
Now let us discuss thefindFirst
examples.
Example-1: Suppose we have a stream of some integers and we are calling
findFirst
method on it.
Stream.of(50, 60, 70).findFirst() .ifPresent(s -> System.out.println(s));
Find more code.
FindFirstDemo1.java
package com.concretepage; import java.util.Arrays; import java.util.List; public class FindFirstDemo1 { public static void main(String[] args) { List<String> list = Arrays.asList("Vijay", "Suresh", "Vinod"); String output = list.stream() .filter(e -> e.startsWith("V")) // Vijay, Vinod .findFirst() //Vijay .orElse("NA"); System.out.println(output); List<Integer> numList = Arrays.asList(31, 32, 33, 34); numList.stream() .filter(n -> n % 2 == 0) // 32, 34 .findFirst() //32 .ifPresent(e -> System.out.println(e)); } }
Vijay 32
Example-2: Find the example of
findFirst
method using IntStream
, LongStream
and DoubleStream
.
FindFirstDemo2.java
package com.concretepage; import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.LongStream; public class FindFirstDemo2 { public static void main(String[] args) { IntStream intStream = IntStream.of(10, 20, 30, 40); intStream.filter(i -> i > 20).findFirst() .ifPresent(i -> System.out.println(i)); LongStream longStream = LongStream.of(100, 200, 300); longStream.filter(l -> l < 250).findFirst() .ifPresent(l -> System.out.println(l)); DoubleStream doubleStream = DoubleStream.of(100.52, 200.55, 300.66); doubleStream.filter(d -> d > 200).findFirst() .ifPresent(l -> System.out.println(l)); } }
30 100 200.55
Example-3: Find the
findFirst
example with parallel stream.
FindFirstDemo3.java
package com.concretepage; import java.util.ArrayList; import java.util.List; public class FindFirstDemo3 { public static void main(String[] args) { List<Employee> list = new ArrayList<>(); list.add(new Employee("Emp A", 3000)); list.add(new Employee("Emp B", 4000)); list.add(new Employee("Emp C", 5000)); list.add(new Employee("Emp D", 6000)); list.parallelStream() .filter(e -> e.getSal() >= 4000 && e.getSal() <= 5000) .mapToInt(e -> e.getSal()) .findFirst() .ifPresent(s -> System.out.println(s)); } } class Employee { private String name; private int sal; public Employee(String name, int sal) { this.name = name; this.sal = sal; } //Sets and Gets }
4000
Example-4: Find the
findFirst
example with null value.
FindFirstDemo4.java
package com.concretepage; import java.util.stream.Stream; public class FindFirstDemo4 { public static void main(String[] args) { Stream.of(null, "A"). findFirst().ifPresent(s -> System.out.println(s)); } }
NullPointerException
.