Saturday, November 5, 2022

[FIXED] How can I use a Predicate in Java?

Issue

How can I use the following Predicate?

Predicate<Integer> isOdd = n -> n % 2 != 0;

My try:

System.out.println(isOdd(5));

Compiler output:

java: cannot find symbol
symbol:   method isOdd(int)
location: class Main

Solution

To invoke a Predicate, use the method called test.

System.out.println(isOdd.test(5));

or

if(isOdd.test(5)){
  // Do something fun
}

This is documented here



Answered By - Dawood ibn Kareem
Answer Checked By - Gilberto Lyons (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.