Predicate<T> Java Example

What is Predicate in Java?

This is a functional interface which can therefore be used as the assignment target for a lambda expression or method reference.

This feature can use in logical condition and return boolean type. Let’s see the example below.

Type Parameters:T - the type of the input to the predicate

ref: https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html

Example

Class Student

How to use it ?

Predicate can apply with

  • Predicate.and(Predecate<T> other)
  • Predicate.or(Predecate<T> other)

Conclusion

It can use well with Java Steam API and lamda expression. Moreover, Can move to central common code and easy to work with unit testing.

--

--

Kachain Jantalat

I am a software engineer that has a passionate in programming and development