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,
Java Predicate Examples:- 1: Check if provided input is of type String
- 2: Check if provided input is of null
- 3: Check if provided input String is declared
- 4: Check if age is over 18 or not
- 5: Check if array contains inputted String.
- 6: Check if provided input has length > 4 or not
- 7: Predicate with changing with default or method
- 8: Predicate with Stream filter
Example 1:
package org.code2care;
import java.util.function.Predicate;
/**
*
* 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
Example 2:
package com.company;
import java.util.function.Predicate;
/**
*
* 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
Example 3:
package com.company;
import java.util.function.Predicate;
/**
*
* 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
Example 4:
package com.company;
import java.util.function.Predicate;
/**
*
* 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
Example 5:
package com.company;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
/**
*
* 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
Example 6:
package com.company;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
/**
*
* Use of Predicate to check if the provided input
* has the length as required
*
*/
public class PredicateExample6 {
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
Example 7:
package com.company;
import java.util.function.Predicate;
/**
*
* Predicate With Chaining with or default function
*
*/
public class PredicateExample7 {
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
Example 8:
package com.company;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* Predicate with Stream filter
*
*/
public class PredicateExample8 {
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;
}
}
}

- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Find MAC address of your laptop device - HowTos
- Deep Dive: Why avoid java.util.Date and Calendar Classes - Java
- List of jars required for Struts2 project - Java
- 'pwd' is not recognized as an internal or external command, operable program or batch file. [Windows] - Bash
- Install GitHub Command Line Tool on Mac - Git
- Java 8 Display time in 12 hour AM PM format - Java
- W3 : character data is not allowed here html validation error - Html
- Android Emulator 5.1.1 not loading on Mac OS X Android Studio - Android-Studio