Java 8 Examples

IntPredicate Interface in Java 8 with examples

Posted On
Posted By admin

In this blog post, I will be explaining how the Java 8 functional interface IntPredicate works. To know more about functional interfaces, you can refer this blog post.

The IntPredicate interface provides a method called test. This method accepts a parameter of Integer data type and returns a boolean. The  IntPredicate interface is a specialization of the Predicate interface. While the Predicate interface accepts any data type, the IntPredicate interface accepts an Integer value. To see an example of the Predicate interface, refer to this blog post.

IntPredicate Example

Consider the following code snippet:

public class IntPredicateDemo {
public static void main(String args[]){
IntPredicate greaterThan10 = (input) -> input > 10;
System.out.println("4 is greater than 10 = "+greaterThan10.test(4));
System.out.println("15 is greater than 10 = "+greaterThan10.test(15));
}
}

Here, the IntPredicate.test method checks if the input number is greater than 10. So when the above code is executed, it will print the following output:

4 is greater than 10 = false 
15 is greater than 10 = true

You can get the source code for this example along with other code for other Java 8 examples at the Github repository here.



If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my new course Learn Java 8 New Features

If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment