Sunday 15 July 2018

Java8: Streams filter example

‘java.util.stream.Stream’  interface provides filter() method, it is used to filter the stream that satisfies given criteria.

Signature of filter method looks like below.
Stream<T> filter(Predicate<? super T> predicate);
Return new stream that satisfies given predicate.

Example 1: Get Employees with even ids.
List<Employee> evenIdEmps = emps.stream().filter(emp -> emp.getId() % 2 == 0).collect(Collectors.toList());

Example 2: Get Employees with firstName 'Janaki'
List<Employee> empsNameJanaki = emps.stream().filter(emp -> emp.getFirstName().equals("Janaki")).collect(Collectors.toList());

Example 3: Find the first employee that has firstName ‘Janaki’.
Optional<Employee> empOpt = emps.stream().filter(emp -> emp.getFirstName().equals("Janaki")).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 + "]";
 }

}

Test.java

package com.sample.app;

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

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 all the employees with even ids */
  System.out.println("\nEmployees with even ids");
  List<Employee> evenIdEmps = emps.stream().filter(emp -> emp.getId() % 2 == 0).collect(Collectors.toList());
  printEmployees(evenIdEmps);

  /* Get all employees with firstName Janaki */
  System.out.println("\nAll the employees that has name Janaki");
  List<Employee> empsNameJanaki = emps.stream().filter(emp -> emp.getFirstName().equals("Janaki"))
    .collect(Collectors.toList());
  printEmployees(empsNameJanaki);

  /* Get first matched employee with firstName Janaki */
  System.out.println("\n First 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());
  }

 }
}

Run Test.java, you can able to 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]

Employees with even ids
Employee [id=2, firstName=Joel, lastName=Chelli]
Employee [id=4, firstName=Janaki, lastName=Sriram]

All the employees that has name Janaki
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]


Previous                                                 Next                                                 Home

No comments:

Post a Comment