Java 8 Predicate examples

Java 8 predicates are functional interface introduced in java 8.
Predicate is single argument function which returns true or false. It has test method which returns true or false.
For example:
package org.arpit.java2blog;
import java.util.function.Predicate;

public class Java8PredicateExample {

 public static void main(String[] args)
 {
  Predicate<Integer>  predicate= i->i>100;
  boolean greaterCheck=predicate.test(200); 
  System.out.println(greaterCheck);
 }
}
So when you run above predicate logic, it will return true.
You can pass Predicate as a function argument too.
package org.arpit.java2blog;
import java.util.function.Predicate;

public class Java8PredicateExample {

 public static void main(String[] args)
 { 
  boolean greaterCheckBoolean=greaterCheck(200,p->p>100); 
  System.out.println(greaterCheckBoolean);
 }

 public static boolean greaterCheck(int number,Predicate<Integer> predicate)
 {
  return predicate.test(number);
 }
}

Filter list using predicate:

 Java 8 stream's function takes Predicate as a argument and can be used to filter the list with the help of predicates.
Lets say you have student class as below:
package org.arpit.java2blog;

public class Student {

 private int id;
 private String name;
 private String gender;
 private int age;
 
 public Student(int id, String name, String gender, int age) {
  super();
  this.id = id;
  this.name = name;
  this.gender = gender;
  this.age = age;
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getGender() {
  return gender;
 }
 public void setGender(String gender) {
  this.gender = gender;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 
 @Override
 public String toString() {
  return "Student [id=" + id + ", name=" + name + ", gender=" + gender
    + ", age=" + age + "]";
 }  
}
Lets create a function which will be used to filter students based on predicates.
 public static List<Student> filterStudents (List<Student> students,Predicate<Student> predicate) {
     return students.stream().filter( predicate ).collect(Collectors.toList());
    }
Lets create a main class as below:
package org.arpit.java2blog;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Java8PredicateExample {

 public static void main(String[] args)
 { 
  List<Student> studentList=createStudentList(); 
  
  // Filter all male student who have age > 18
  Predicate<Student> predicate1= s-> s.getGender().equalsIgnoreCase("M") && s.getAge() > 18;
  List<Student> students1=filterStudents(studentList, predicate1);
         System.out.println("Male students having age > 18 are :"+students1);
  
                // Filer all female student who have age < 18
  Predicate<Student> predicate2= s-> s.getGender().equalsIgnoreCase("F") && s.getAge() < 18;
  List<Student> students2=filterStudents(studentList, predicate2);
  System.out.println("Female students having age < 18 are :"+students2);
 }

 public static List<Student> filterStudents (List<Student> students, Predicate<Student> predicate) {
        return students.stream().filter( predicate ).collect(Collectors.toList());
    }
 
 public static List<Student> createStudentList()
 {
  List<Student> studentList=new ArrayList<Student>();
  Student s1=new Student(1, "Arpit", "M", 19);
  Student s2=new Student(2, "John", "M", 17);
  Student s3=new Student(3, "Mary", "F", 14);
  Student s4=new Student(4, "Martin", "M", 21);
  Student s5=new Student(5, "Monica", "F", 16);
  Student s6=new Student(6, "Ally", "F", 20);
  
  studentList.add(s1);
  studentList.add(s2);
  studentList.add(s3);
  studentList.add(s4);
  studentList.add(s5);
  studentList.add(s6);
  return studentList;
 }
}
When you run above program, you will get below output:
Male students having age > 18 are :[Student [id=1, name=Arpit, gender=M, age=19], Student [id=4, name=Martin, gender=M, age=21]]
Female students having age < 18 are :[Student [id=3, name=Mary, gender=F, age=14], Student [id=5, name=Monica, gender=F, age=16]]


Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012