Sitemap
Java Fusion

Java Fusion

Java enthusiast crafting code with passion. enthusiastic about creating robust, scalable applications. Let's code, innovate, and build the future together!

What is the predicate in Java?

4 min readApr 3, 2024

--

In Java 8, the Predicate is a functional interface that belongs to the java. util.function package.

Press enter or click to view image in full size
Bhairab Patra

Predicate represents a boolean-valued function that takes one argument and returns a boolean result.

This interface is commonly used for defining conditions or filters when working with collections or performing other conditional checks.

The Predicate interface has one abstract method and that is test(T t)

@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}

Here, T is the type of input argument to the predicate, and the test method evaluates the predicate with this argument and returns a boolean result.

Here’s an example of using a Predicate in Java 8

package com.shorts.java8.predicate;

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

public class PredicateTest {

// Check the number is even
public boolean isEven() {
Predicate<Integer> pr = (n) -> n % 2 == 0;
return pr.test(20);
}

// Print the name start with char 'A'

public void checkPersonNameStartWIthA() {
String[] nameList = {"anil", "kuna", "kanha", "ajay"};

Predicate<String> predicate = (name) -> name.startsWith("a");
for (String names : nameList) {
if (predicate.test(names.toLowerCase())) {
System.out.println(names);
}
}
}

// Print the name of the person who is eligible to cast a vote
public void canVote() {
// Create a pojo class with name and age
List<Person> person = new ArrayList<>();

person.add(new Person("muna", 20));
person.add(new Person("Raju", 10));
person.add(new Person("sita", 19));
person.add(new Person("Nick", 10));

Predicate<Person> predicate = (p) -> p.getAge() >= 18;
for (Person p : person) {
if (predicate.test(p)) {
System.out.println(p.getName() + " can vote");
}
}
}

public static void main(String[] args) {
PredicateTest predicateTest = new PredicateTest();
System.out.println(predicateTest.isEven());
predicateTest.checkPersonNameStartWIthA();
predicateTest.canVote();
}
}

Example 2

package com.shorts.java8.optionalclass;

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

public class PredicateExample {

public void predicateTest() {
List<Integer> number = new ArrayList<>();
number.add(10);
number.add(20);
number.add(30);
number.add(90);
number.add(200);

Predicate<Integer> predicate = (n) -> n >= 30;

List<Integer> filterNumber = filterItem(number, predicate);

for (Integer result : filterNumber) {
System.out.println("The result is " + result);
}

}

private List<Integer> filterItem(List<Integer> number, Predicate<Integer> predicate) {
List<Integer> newList = new ArrayList<>();
for (Integer n : number) {
if (predicate.test(n)) { // According to the condition result value will store in newList
newList.add(n);
}
}
return newList;
}

public static void main(String[] args) {
PredicateExample predicateExample = new PredicateExample();
predicateExample.predicateTest();
}
}

The negate() method is specifically used to negate a predicate, effectively reversing its logic.

Get Java Fusion’s stories in your inbox

Join Medium for free to get updates from this writer.

Here’s an example to demonstrate Predicate joining and the negate() method:

import java.util.function.Predicate;

public class Main {
public static void main(String[] args) {
Predicate<Integer> isEven = num -> num % 2 == 0;

// Negating a predicate
Predicate<Integer> isNotEven = isEven.negate();

// Testing negated predicate
System.out.println("Is 3 not even? " + isNotEven.test(3)); // Output: true
System.out.println("Is 6 not even? " + isNotEven.test(6)); // Output: false
}
}

What is Predicate joining in Java?

Predicate joining in Java refers to the process of combining multiple predicates into a single predicate using logical operators like AND (and), OR (or), and NOT (negate).

  • and() Returns a composed predicate when two predicates are TRUE.
  • or(): Returns a composed predicate when one of the predicates is TRUE
  • negate(): Returns a predicate that represents the logical negation of this predicate.

Here’s an example to illustrate predicate joining

Create POJO for person then write logic for PredicateJoiningTest


public class Person {

String name;
int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getter & Setter
}

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

public class PredicateJoiningTest {

public void predicateJoiningExample(){

List<Person> p = new ArrayList<>();
p.add(new Person("muna", 30));
p.add(new Person("priyanka", 40));
p.add(new Person("Kuna", 34));
p.add(new Person("Runa", 23));
p.add(new Person("Jack", 50));

Predicate<Person> predicate = (age) -> age.getAge() >= 20;
Predicate<Person> predicate1 = (age) -> age.getAge() <= 45;

for(Person person: p){
// we can use or predicate.and(predicate1).test(person)
if(predicate.and(predicate1).test(person)){
System.out.println(person.getName());
}
}
}

public static void main(String[] args){
PredicateJoiningTest predicatejoiningTest = new PredicateJoiningTest();
predicatejoiningTest.predicateJoiningExample();
}
}

You can use predicate joining to create complex conditions for filtering or processing data based on multiple criteria.

What is BiPredicate in Java?

  • A BiPredicate is a functional interface introduced in Java 8 as part of the java.util.function package.
  • It represents a predicate that takes two arguments and returns a boolean result.
  • The two arguments can be of any data type, and the return type is always boolean.

The BiPredicate interface has one abstract method:

boolean test(T t, U u);

Here, T and U are the types of the two input arguments to the predicate, and the test method evaluates the predicate with these arguments and returns a boolean result.

package com.shorts.java8.optionalclass;

import java.util.function.BiPredicate;

public class BiPredicateTest {

public boolean BiPredicateExample() {
BiPredicate<Integer, Integer> biPredicate = (v1, v2) -> v1 * v2 % 2 == 0;
return biPredicate.test(5, 1);
}

public static void main(String[] args) {
BiPredicateTest biPredicate = new BiPredicateTest();
System.out.println(biPredicate.BiPredicateExample());
}
}

BiPredicate is commonly used in scenarios where you need to apply a boolean condition that involves two input values, such as filtering data based on multiple criteria or checking relationships between two objects.

Thank you !!!

--

--