Saturday, 14 July 2018

Java8 Streams anyMatch example

'java.util.stream.Stream' interface provides 'anyMatch' method, it return true if any one element of the stream matches to given predicate.

boolean anyMatch(Predicate<? super T> predicate)
For example, I defined Employee class like below.

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

private static List<Employee> emps = new ArrayList<> ();

Below snippet checks whether employee with id 5 exists or not.

if (emps.stream().anyMatch(emp -> emp.getId() == 5)) {
         System.out.println("Employee with id 5 exists");
} else {
         System.out.println("Employee with id 5 is not exist");
}

Below snippet checks whether employee with firstName ‘Ram’ exists or not.

if (emps.stream().anyMatch(emp -> emp.getFirstName().equals("Ram"))) {
         System.out.println("Employee with first name Ram exists");
} else {
         System.out.println("Employee with first name Ram is not exist");
}

Below snippet checks whether employee with lastName ‘Battu’ exists or not.
if (emps.stream().anyMatch(emp -> emp.getLastName().equals("Battu"))) {
         System.out.println("Employee with last name Battu exists");
} else {
         System.out.println("Employee with last name Battu is not exist");
}



Find the below working application.


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;
 }

}

Test.java

package com.sample.app;

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

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);
 }

 public static void main(String args[]) {

  if (emps.stream().anyMatch(emp -> emp.getId() == 5)) {
   System.out.println("Employee with id 5 exists");
  } else {
   System.out.println("Employee with id 5 is not exist");
  }

  if (emps.stream().anyMatch(emp -> emp.getFirstName().equals("Ram"))) {
   System.out.println("Employee with first name Ram exists");
  } else {
   System.out.println("Employee with first name Ram is not exist");
  }

  if (emps.stream().anyMatch(emp -> emp.getLastName().equals("Battu"))) {
   System.out.println("Employee with last name Battu exists");
  } else {
   System.out.println("Employee with last name Battu is not exist");
  }

 }
}


Output

Employee with id 5 exists
Employee with first name Ram is not exist
Employee with last name Battu exists


Previous                                                 Next                                                 Home

No comments:

Post a Comment