Java Predicate

Kaan Ardar
2 min readNov 23, 2020

We can think of it as a function that processes the entered parameter or parameters and returns them whether they are true or false.
- f(x) = true | false

Java Predicate Class

The predicate is a functional interface. The predicate uses for filter and I think, helps improve the readability of your code. for example one such method is filter() method from Stream interface.

/**
* Returns a stream consisting of the elements of this stream that match
* the given predicate.
*
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
* operation</a>.
*
*
@param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
* <a href="package-summary.html#Statelessness">stateless</a>
* predicate to apply to each element to determine if it
* should be included
*
@return the new stream
*/
Stream<T> filter(Predicate<? super T> predicate);

Sample Predicate Usage

we have a user class as below:

package sample.predicate;

import lombok.*;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User {

private String id;
private String name;
private Integer age;
private String country;

}

We created a predicate class for the user class for readability.

package sample.predicate;

import sample.predicate.User;

import java.util.Objects;
import java.util.function.Predicate;

public class UserPredicate {

public static Predicate<User> findCountry(String country){
return u -> u.getCountry().equalsIgnoreCase(country);
}

public static Predicate<User> filterAge(Integer age){
return u -> u.getAge() >= age;
}

public static Predicate<User> checkNonNullAge(){
return u -> Objects.nonNull(u.getAge());
}

public static Predicate<User> filterAndCheckAge(Integer age){
return u -> Objects.nonNull(u.getAge()) && u.getAge() >= age;
}

}

You can the test class as below: we did filter country and age in the user collection.

package sample.predicate;

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

public class PredicateTest {

public static void main(String[] args) {
List<User> users = Arrays.asList(
User.builder().id("1").name("kaan").age(28).country("TR").build(),
User.builder().id("2").name("Ardar").age(28).country("TR").build(),
User.builder().id("3").name("Mert").age(35).country("DE").build(),
User.builder().id("4").name("Sevki").age(32).country("GB").build(),
User.builder().id("5").name("Emre").age(35).country("FR").build(),
User.builder().id("6").name("Sinan").age(null).country("SY").build()
);

//we want to filter TR users.
List<User> trUsers = users.stream()
.filter(UserPredicate.findCountry("TR"))
.collect(Collectors.toList());

//we want to filter age
List<User> ageUser = users.stream()
.filter(UserPredicate.filterAndCheckAge(33))
.collect(Collectors.toList());

System.out.println("TR users : ");
System.out.println(trUsers.toString());

System.out.println("Filter age users : ");
System.out.println(ageUser.toString());

}

}

I wish good and happy coding. [KA]

--

--