Java Stream findFirst()

By Arvind Rai, May 15, 2020
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.
Find the findFirst declaration from Java doc.
Optional<T> findFirst() 
The 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.

Now let us discuss the findFirst 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)); 
The output is 50 which is the first element of the stream.

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));
  }
} 
Output
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));
  }
} 
Output
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
} 
Output
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));
  }
} 
Output will be NullPointerException.

Reference

Java doc: Stream
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us