PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 5, 2022

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

 November 05, 2022     java, lambda, predicate     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing