Predicate and BiPredicate in Java

Photo by Jonas Jacobsson on Unsplash

Introduction

Predicate

Functional Interface

Example

  • Employees who are Male and age greater than 25
  • Employees who are Female and are Managers.
Male and age greater than 25
Id: 2, Name: Steve Rogers, Age: 28, Gender: MALE, Role: MANAGER
Female and Manager
Id: 3, Name: Natasha Romanoff, Age: 23, Gender: FEMALE, Role: MANAGER

Predicate Composition

and()

or()

negate()

isEqual()

and() operation
true
false
or() operation
true
true
negate() operation
false
isEqual() operation
true

BiPredicate

10 divisible by 2: true
5 divisible by 3: false
8 divisible by 4: true

Conclusion

  • Predicate is a functional interface in Java that accepts a single input and can return a boolean value.
  • BiPredicate is a functional interface in Java that accepts two inputs and can return a boolean value.
  • Predicate and BiPredicate are usually used to apply in a filter for a collection of objects.

Thank you 🤘

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store