Predicates

  • Eclipse: Oxygen
  • Java: 1.8

The predicate is a simple Boolean function that evaluates the function to true or false according to the assigned condition.

The predicate is a functional interface and can, therefore, be used as the assignment target for a lambda expression or method reference.

The following example demonstrates how to use IntPredicate. The IntPredicate interface represents an operation that takes a single argument of type int and returns a boolean result.

Output

true

 The following example demonstrates how to use the test() method of the IntPredicate interface with a lambda expression. Here we use the default method and().

Output

false

Here we use the default methods or() of the Predicate interface and check the output.

Output

true

The additional use of predicate along with examples is given here. Where we are converting above same logic into a method.

The following code demonstrates how to create Predicate from method reference and lambda.

Output

The input is less Than 18 and more than 10

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu