Predicate

Example:-1

Check whether the given number is greater than 20 or not?

Logic without Lambda:

public class Test {
    public static void main(String[] args) {
        System.out.println(test(23));
    }

    public static boolean test(int number) {
        if (number > 20) {
            return true;
        } else {
            return false;
        }
    }
}
      

Logic with Lambda:

import java.util.function.Predicate;

public class Test {
    public static void main(String[] args) {
        Predicate<Integer> predicate = (number) -> number > 20;
        System.out.println(predicate.test(40));
    }
}
      

Write a program to check if a person's name has 7 characters or not.

import java.util.function.Predicate;

public class Test {
    public static void main(String[] args) {
        Predicate<String> predicate = (name) -> name.length() >= 7;
        System.out.println(predicate.test("Why are you Drinking"));
    }
}
      

Write a program to print names starting with the character 'A' using lambda from the names below.

import java.util.function.Predicate;

public class NameListPrinting {
    public static void main(String[] args) {
        String[] names = { "Sonu", "Trilochan", "Akash", "Manoranjan", "Rajendra", "Ommprakash", "Milan", "Anand" };
        Predicate<String> predicate = (name) -> name.startsWith("A");
        for (String name : names) {
            if (predicate.test(name)) {
                System.out.println(name);
            }
        }
    }
}
      

Write a Java program to print the names of persons eligible to vote from the list using Lambda.

import java.util.ArrayList;
import java.util.List;

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }
}

public class Main {
    public static void main(String[] args) {
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("Sonu", 25));
        persons.add(new Person("Monu", 17));
        persons.add(new Person("Raju", 30));
        persons.add(new Person("David", 15));
        persons.add(new Person("Akash", 21));

        System.out.println("Persons eligible to vote:");

        Predicate<Integer> voter = (age) -> age >= 18;
        for (Person person : persons) {
            if (voter.test(person.getAge())) {
                System.out.println(person.getName());
            }
        }
    }
}
      

What is Predicate Joining

Write a Java program to identify who is eligible for marriage

public class PredicateJoinApp {

  public static void main(String[] args) {
      Person p1 = new Person("Raju", 21);
      Person p2 = new Person("Sonu", 23);
      Person p3 = new Person("Monu", 67);
      Person p4 = new Person("Trilochan", 14);
      Person p5 = new Person("Milan", 26);

      List<Person> persons = Arrays.asList(p1, p2, p3, p4, p5);

      Predicate<Person> ageLowerLimit = (p) -> p.getAge() >= 21;
      Predicate<Person> ageUpperLimit = (p) -> p.getAge() <= 64;

      for (Person p : persons) {
          Predicate<Person> predicate = ageLowerLimit.and(ageUpperLimit);
          boolean isEligibleForMarriage = predicate.test(p);
          if (isEligibleForMarriage) {
              System.out.println(p.getName());
          }
      }
  }
}
      

We can add multiple predicates as below

Predicate finalPredicate1 = p1.and(p2).and(p3).and(p4).and(p5);
Predicate finalPredicate2 = p1.or(p2).or(p3).or(p4).or(p5);
      

Note: Here p1, p2, p3, p4, and p5 are predicates

We can execute the final predicate by calling the test() method

boolean status1 = finalPredicate1.test(input);
boolean status2 = finalPredicate2.test(input);
      

What is BiPredicate Interface

public class Demo {

    public static void main(String[] args) {
        BiPredicate<Integer, Integer> biPredicate = (i, j) -> i + j >= 100;
        System.out.println(biPredicate.test(10, 200));
    }
}