Java 8 Predicate Functional Interface Examples


In Java 8 java.util.function package you will find the Predicate.java Functional Interface which is a boolean-valued function which takes one argument and returns a boolean, lets see some examples,

  • Example 1: Check if provided input is of type String
  • Example 2: Check if provided input is of null
  • Example 3: Check if provided input String is declared
  • Example 4: Check if age is over 18 or not
  • Example 5: Check if array contains inputted String.
  • Example 6: Check if provided input has length > 4 or not
  • Example 7: Predicate with changing with default or method
  • Example 8: Predicate with Stream filter

Predicate Example 1:

package org.code2care;

import java.util.function.Predicate;

/**
 * 
 * Predicate Example 1:
 * 
 * Use of Predicate to check if the provided input 
 * to test function is a String or not
 * 
 */
public class PredicateExample1 {

    public static void main(String[] args) {

        String string = "Java";
        int integer = 10;
        Boolean bool = true;

        Predicate<Object> isString = object -> (object instanceof String);

        System.out.println(isString.test(string));
        System.out.println(isString.test(integer));
        System.out.println(isString.test(bool));

    }
}
Output:

true
false
false



Predicate Example 2:

package com.company;

import java.util.function.Predicate;

/**
 *
 * Predicate Example 2:
 *
 * Use of Predicate to check if the provided input
 * as is null or not
 *
 */
public class PredicateExample2 {

    public static void main(String[] args) {

        String str1 = null;
        String str2 = "Java";

        Predicate<Object> isDefined = s -> (s == null);

        System.out.println(isDefined.test(str1));
        System.out.println(isDefined.test(str2));

    }
}
Output:

true
false



Predicate Example 3:

package com.company;

import java.util.function.Predicate;

/**
 *
 * Predicate Example 3:
 *
 * Use of Predicate to check if the provided input
 * to declared or not
 *
 */
public class PredicateExample3 {

    public static void main(String[] args) {

        String str1 = null;
        String str2 = " ";
        String str3 = "";
        String str4 = "Java";

        Predicate<String> isDefined = s -> (s == null || "".equals(s.trim()));

        System.out.println(isDefined.test(str1));
        System.out.println(isDefined.test(str2));
        System.out.println(isDefined.test(str3));
        System.out.println(isDefined.test(str4));

    }
}
Output:

true
true
true
false



Predicate Example 4:

package com.company;

import java.util.function.Predicate;

/**
 *
 * Predicate Example 4:
 *
 * Use of Predicate to check if the provided input
 * as age is greater than 18 or not
 *
 */
public class PredicateExample4 {

    public static void main(String[] args) {

        int age1 = 12;
        int age2 = 18;
        int age3 = 22;

        Predicate<Integer> isAbove18 = s -> (s > 18);

        System.out.println(isAbove18.test(age1));
        System.out.println(isAbove18.test(age2));
        System.out.println(isAbove18.test(age3));

    }
}
Output:

false
false
true



Predicate Example 5:

package com.company;

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

/**
 *
 * Predicate Example 5:
 *
 * Use of Predicate to check if the provided input
 * is available in a Collection List or not
 *
 */
public class PredicateExample5 {

    public static void main(String[] args) {

        List<String> countries = new ArrayList<>();
        countries.add("Japan");
        countries.add("China");
        countries.add("USA");
        countries.add("France");
        countries.add("Australia");
        countries.add("Sweden");

        Predicate<String> isInCountriesList = country -> (countries.contains(country));

        String country1 = "Canada";
        String country2 = "India";
        String country3 = "Sweden";

        System.out.println(isInCountriesList.test(country1));
        System.out.println(isInCountriesList.test(country2));
        System.out.println(isInCountriesList.test(country3));

    }
}
Output:

false
false
true



Predicate Example 6:

package com.company;

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

/**
 *
 * Predicate Example 6:
 *
 * Use of Predicate to check if the provided input
 * has the length as required
 *
 */
public class PredicateExample3 {

    public static void main(String[] args) {

        String s1 = "Java";
        String s2 = "Python";
        String s3 = "PHP";
        String s4 = "Scala";
        String s5 = "Swift";

        Predicate<String> isLengthGreaterThan4 = string -> (string.length() > 4);
        
        System.out.println(isLengthGreaterThan4.test(s1));
        System.out.println(isLengthGreaterThan4.test(s2));
        System.out.println(isLengthGreaterThan4.test(s3));
        System.out.println(isLengthGreaterThan4.test(s4));
        System.out.println(isLengthGreaterThan4.test(s5));

    }
}
Output:

false
false
false
true
true



Predicate Example 7:

package com.company;

import java.util.function.Predicate;

/**
 *
 * Predicate Example 7:
 *
 * Predicate With Chaining with or default function
 *
 */
public class PredicateExample3 {

    public static void main(String[] args) {

        String s1 = "Cool";
        String s2 = "Dude";
        String s3 = "Awesome";

        Predicate<String> startsWithC = (string) -> (string.toLowerCase().startsWith("c"));
        Predicate<String> startsWithD = (string) -> (string.toLowerCase().startsWith("d"));
        Predicate<String> startsWithCorD = startsWithC.or(startsWithD);

        System.out.println(startsWithCorD.test(s1));
        System.out.println(startsWithCorD.test(s2));
        System.out.println(startsWithCorD.test(s3));

    }
}
Output:

true
true
false



Predicate Example 8:

package com.company;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * Predicate Example 7:
 *
 * Predicate with Stream filter
 *
 */
public class PredicateExample3 {

    public static void main(String[] args) {

       Employee emp1 = new Employee(1,"John",34,2000);
       Employee emp2 = new Employee(2,"Alex",22,5000);
       Employee emp3 = new Employee(3,"Brian",23,5000);
       Employee emp4 = new Employee(3,"Bob",23,7500);

       List<Employee> employeeList = new ArrayList<>();
       employeeList.add(emp1);
       employeeList.add(emp2);
       employeeList.add(emp3);
       employeeList.add(emp4);

        ArrayList<Employee> allEmpWithAgeGt22AndNameStartWithB = (ArrayList<Employee>) employeeList.stream().filter(emp -> emp.getEmpAge() > 22 && emp.getEmpName().startsWith("B")).collect(Collectors.toList());

        for (Employee emp: allEmpWithAgeGt22AndNameStartWithB) {
            System.out.println(emp.getEmpName()+","+emp.getEmpAge()+","+emp.getEmpSalary());
        }

    }


   static class Employee {

        int empId;
        String empName;
        int empAge;
        int empSalary;

        public Employee(int empId, String empName, int empAge, int empSalary) {
            this.empId = empId;
            this.empName = empName;
            this.empAge = empAge;
            this.empSalary = empSalary;
        }

        public int getEmpId() {
            return empId;
        }

        public void setEmpId(int empId) {
            this.empId = empId;
        }

        public String getEmpName() {
            return empName;
        }

        public void setEmpName(String empName) {
            this.empName = empName;
        }

        public int getEmpAge() {
            return empAge;
        }

        public void setEmpAge(int empAge) {
            this.empAge = empAge;
        }

        public int getEmpSalary() {
            return empSalary;
        }

        public void setEmpSalary(int empSalary) {
            this.empSalary = empSalary;
        }
    }

}
Java 8 Predicate Functional Interfaces Examples
Advertisements
Try Out Code2care Dev Tools:

Advertisements



#Android #Java #Linux #Microsoft #Google #Python #MacOS #Notepad++ #Microsoft Teams #CSS #PHP #SharePoint #Html #Linux #C-Program #Bootstrap #jQuery #Sublime #Android-Studio #Facebook #Eclipse #WhatsApp #News #MySQL #Json #HowTos #JavaScript #FTP #S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency