Java 8 Predicate examples

Previous
Next
Java 8 predicates are functional interface introduced in java 8.
Predicate is single argument function which returns true or false. It has test method which returns true or false.
For example:

So when you run above predicate logic, it will return true.
You can pass Predicate as a function argument too.

Filter list using predicate:

Java 8 stream’s function takes Predicate as a argument and can be used to filter the list with the help of predicates.
Lets say you have student class as below:

Lets create a function which will be used to filter students based on predicates.

Lets create a main class as below:

When you run above program, you will get below output:

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog