Sunday 15 July 2018

java8 streams findFirst Example

'java.uti.stream.Stream' interface provides 'findFirst' method, it return an Optional describing the first element of this stream, or an empty Optional if the stream is empty.

Find the signature of findFirst method.
Optional<T> findFirst()

Example 1: Get first employee with firstName equal to 'Janaki'
Optional<Employee> empOpt = emps.stream().filter(emp -> emp.getFirstName().equals("Janaki")).findFirst();

if (empOpt.isPresent()) {
         System.out.println(empOpt.get());
}

Example 2: Get first employee with id is odd number.
empOpt = emps.stream().filter(emp -> emp.getId() % 2 != 0).findFirst();

if (empOpt.isPresent()) {
         System.out.println(empOpt.get());
}



Find the below working example.

Employee.java
package com.sample.app.model;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;

 public Employee(int id, String firstName, String lastName) {
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
 }

 public Formatter formatter() {
  return new Formatter();
 }

 public class Formatter {
  public String formattedInfo() {
   String firstName = Employee.this.firstName;
   String lastName = Employee.this.lastName;
   return firstName.toUpperCase() + "," + lastName.toUpperCase();

  }
 }

}

Test.java
package com.sample.app;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import com.sample.app.model.Employee;

public class Test {

 private static List<Employee> emps;

 static {
  loadEmployees();
 }

 private static void loadEmployees() {
  Employee emp1 = new Employee(1, "Krishna", "Gurram");
  Employee emp2 = new Employee(2, "Joel", "Chelli");
  Employee emp3 = new Employee(3, "Gopi", "Battu");
  Employee emp4 = new Employee(4, "Janaki", "Sriram");
  Employee emp5 = new Employee(5, "Janaki", "Maj");

  emps = Arrays.asList(emp1, emp2, emp3, emp4, emp5);
 }

 private static void printEmployees(List<Employee> emps) {
  emps.stream().forEach(System.out::println);
 }

 public static void main(String args[]) {
  System.out.println("All Employees");
  printEmployees(emps);

  /* Get first matched employee with firstName Janaki */
  System.out.println("\nFirst employee with matched firstName Janaki");
  Optional<Employee> empOpt = emps.stream().filter(emp -> emp.getFirstName().equals("Janaki")).findFirst();

  if (empOpt.isPresent()) {
   System.out.println(empOpt.get());
  }

  /* Get first matched employee with odd id */
  System.out.println("\nFirst employee with matched odd number id");
  empOpt = emps.stream().filter(emp -> emp.getId() % 2 != 0).findFirst();

  if (empOpt.isPresent()) {
   System.out.println(empOpt.get());
  }

 }
}

Run Test.java, you can see below messages in console.
All Employees
Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=2, firstName=Joel, lastName=Chelli]
Employee [id=3, firstName=Gopi, lastName=Battu]
Employee [id=4, firstName=Janaki, lastName=Sriram]
Employee [id=5, firstName=Janaki, lastName=Maj]

First employee with matched firstName Janaki
Employee [id=4, firstName=Janaki, lastName=Sriram]

First employee with matched odd number id
Employee [id=1, firstName=Krishna, lastName=Gurram]



Previous                                                 Next                                                 Home

No comments:

Post a Comment