Java 8 Stream findAny & findFirst method Example

7 years ago Lalit Bhagtani 0

In this tutorial, we will learn about Java 8 stream findAny and findFirst method.

java 8 stream findAny

Note :- To keep this tutorial short and concise, Employee Bean used in the example is not given here, you can check it here.

findAny() method :-

This method is called on Stream object, it returns an Optional object containing any one element of the stream, or an empty Optional object if the stream is empty.

Syntax :- Optional<T> findAny()

Reference :-  findAny() method JavaDocs 

Problem Statement :-

You have given list of employees, find any employee whose designation is “Manager“.

Java 7 :- To implement this, first iterate through the list of employees, check designation of each employee object and return any element whose designation is manager.


  public static void main(String[] args){
    List<Employee> empList = new ArrayList<Employee>();
    empList.add(new Employee(1,"Robert",35,"Manager"));
    empList.add(new Employee(2,"Martin",35,"General Manager"));
    empList.add(new Employee(1,"Jack",25,"Manager"));
 
    Employee temp = null;
    for(Employee emp : empList){
      if("Manager".equalsIgnoreCase(emp.getDesignation())){
        temp = emp;
      }
    }
    System.out.println("Name :- " + temp.getName() + " , Designation :- " + temp.getDesignation()); 
  }

Java 8 :- Let’s see the implementation of above problem using streams API.


  public static void main(String[] args){
    List<Employee> empList = new ArrayList<Employee>();
    empList.add(new Employee(1,"Robert",35,"Manager"));
    empList.add(new Employee(2,"Martin",35,"General Manager"));
    empList.add(new Employee(1,"Jack",25,"Manager"));
 
    Optional<Employee> optional = empList.stream().filter( e -> "Manager".equalsIgnoreCase(e.getDesignation())).findAny();
 
    System.out.println("Name :- " + optional.get().getName() + " , Designation :- " + optional.get().getDesignation()); 
 }

Result :- 

 Name :- Robert , Designation :- Manager

findFirst() method :-

This method is called on Stream object, it returns an Optional object containing first element of this stream, or an empty Optional if the stream is empty.

Syntax :- Optional<T> findFirst()

Reference :-  findFirst() method JavaDocs 

Problem Statement :-

You have given list of employees, find first employee whose age is 32.

Java 7 :- To implement this, first iterate through the list of employees, check age of each employee object and return the first element whose age is 32.


  public static void main(String[] args){
    List<Employee> empList = new ArrayList<Employee>();
    empList.add(new Employee(1,"Robert",35,"Manager"));
    empList.add(new Employee(2,"Martin",32,"General Manager"));
    empList.add(new Employee(1,"Jack",25,"Manager"));
    empList.add(new Employee(1,"Dinesh",32,"Manager"));
 
    Employee temp = null;
    for(Employee emp : empList){
      if(emp.getAge() == 32){
        temp = emp;
        break;
      }
    }
    System.out.println("Name :- " + temp.getName() + " , Designation :- " + temp.getDesignation()); 
  }  

Java 8 :- Let’s see the implementation of above problem using streams API.


  public static void main(String[] args){
    List<Employee> empList = new ArrayList<Employee>();
    empList.add(new Employee(1,"Robert",35,"Manager"));
    empList.add(new Employee(2,"Martin",32,"General Manager"));
    empList.add(new Employee(1,"Jack",25,"Manager"));
    empList.add(new Employee(1,"Dinesh",32,"Manager"));
 
    Optional<Employee> optional = empList.stream().filter( e -> e.getAge() == 32).findFirst();
 
    System.out.println("Name :- " + optional.get().getName() + " , Designation :- " + optional.get().getDesignation()); 
  }

Result :- 

 Name :- Martin , Designation :- General Manager

That’s all for Java 8 Stream findAny & findFirst method Example. If you liked it, please share your thoughts in comments section and share it with others too.