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

More Posts related to Java,
- Drop table using Java JDBC Template
- Java - Check if array contains the value
- YAML Parser using Java Jackson Library Example
- Java Jackson ObjectMapper Class with Examples
- Get Client IP address from HTTP Response in Java
- How to Word-Warp Console logs in IntelliJ
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Setting Java_Home Environment variable on Windows Operating System
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Java SE JDBC Select Statement Example
- How to extract Java Jar/War/Ear files in Linux
- java: unclosed string literal [Error]
- [Solution] Exception in thread main java.util.EmptyStackException
- Read YAML file Java Jackson Library
- What Java version is used for Minecraft 1.18
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Program] How to read three different values using Scanner in Java
- Java 8 Predicate Functional Interface Examples
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Convert String Date to Date Object in Java
- Struts 2 Hello World Example in Eclipse
- Read a file using Java 8 Stream
- Java - How to set custom thread name?
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- java: ']' expected error [fixed]
More Posts:
- GitHub: How to Search Code - Git
- Cannot create new Microsoft Team using PowerShell, even as Admin - Teams
- Base64 Encoding Decoding in Python Programming - Python
- How to know docker Engine details - Docker
- How to Share Microsoft SharePoint Site with Users or Groups - SharePoint
- Display (Show) bookmarks bar Safari - HowTos
- How to switch Eclipse IDE Workspace - Eclipse
- Android Emulator cannot be opened because the developer cannot be verified. [M1 Mac] - Android