Saturday, 15 August 2015

Predicate interface

“java.util.function.Predicate” is a functional interface, contains one functional method ‘test’.

boolean test(T t)
Evaluates predicate on given argument.
Following application shown an example of Predicate interface.

import java.util.Objects;

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

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

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

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }

 @Override
 public boolean equals(Object employee) {
  if (Objects.isNull(employee))
   return false;

  if (!(employee instanceof Employee))
   return false;

  Employee emp = (Employee) employee;

  return id == emp.id;
 }

 @Override
 public int hashCode() {
  return Objects.hash(id, firstName, lastName, age);
 }

 @Override
 public String toString() {
  return String.format("%s(%s,%d)", firstName, city, age);
 }

}

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class EmployeeTest {

 public static <T> List<T> processEmployees(List<T> employees,
   Predicate<T> predicate) {
  List<T> tmpList = new ArrayList<>();
  for (T emp : employees) {
   if (predicate.test(emp)) {
    tmpList.add(emp);
   }
  }
  return tmpList;
 }

 public static void main(String args[]) {
  Employee emp1 = new Employee(1, "Hari Krishna", "Gurram", 26,
    "Bangalore");
  Employee emp2 = new Employee(2, "Joel", "Chelli", 27, "Hyderabad");
  Employee emp3 = new Employee(3, "Shanmukh", "Kummary", 28, "Chennai");
  Employee emp4 = new Employee(4, "Harika", "Raghuram", 27, "Chennai");
  Employee emp5 = new Employee(5, "Sudheer", "Ganji", 27, "Bangalore");
  Employee emp6 = new Employee(6, "Rama Krishna", "Gurram", 27,
    "Bangalore");
  Employee emp7 = new Employee(7, "PTR", "PTR", 27, "Hyderabad");
  Employee emp8 = new Employee(8, "Siva krishna", "Ponnam", 28,
    "Hyderabad");

  List<Employee> employees = new ArrayList<>();

  employees.add(emp1);
  employees.add(emp2);
  employees.add(emp3);
  employees.add(emp4);
  employees.add(emp5);
  employees.add(emp6);
  employees.add(emp7);
  employees.add(emp8);

  List<Employee> ageGreat27;
  List<Employee> firstNameStartsWithS;

  ageGreat27 = processEmployees(employees, (emp) -> emp.getAge() > 27);
  firstNameStartsWithS = processEmployees(employees, (emp) -> emp
    .getFirstName().startsWith("S"));

  System.out.println(ageGreat27);
  System.out.println(firstNameStartsWithS);
 }
}


Output

[Shanmukh(Chennai,28), Siva krishna(Hyderabad,28)]
[Shanmukh(Chennai,28), Sudheer(Bangalore,27), Siva krishna(Hyderabad,28)]

public static <T> List<T> processEmployees(List<T> employees,Predicate<T> predicate) {
         List<T> tmpList = new ArrayList<>();
         for (T emp : employees) {
                  if (predicate.test(emp)) {
                           tmpList.add(emp);
                  }
         }
         return tmpList;
}

Above method takes list of objects, predicate as input and check whether an object matches given predicate or not. If an object satisfies given predicate, it is added to list else not.

ageGreat27 = processEmployees(employees, (emp) -> emp.getAge() > 27);
Above statement list all employees whose age is > 27.

firstNameStartsWithS = processEmployees(employees, (emp) -> emp.getFirstName().startsWith("S"));

Above statement list all employees whose first name starts with S.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment