Java 8 IntPredicate Interface
7 years ago Lalit Bhagtani 0
In this tutorial, we will learn about Java 8 IntPredicate interface.
IntPredicate Interface :-
IntPredicate ( java.util.function.IntPredicate ) is a functional interface that has one abstract method and three default method declared in it. This interface is a specialised form of Predicate interface.
Abstract method :-
boolean test ( int value ) :- The abstract method test ( int value ) is a functional method. It accepts one int value as an argument and return boolean value. It represents a conditional operation, where a passed int value is tested against a specific int value to give a boolean ( True or False ) result.
Default method :-
- default IntPredicate<T> and ( IntPredicate <? super T > other ) :- It returns an intpredicate that represents short circuiting logical AND of this intpredicate ( intpredicate on which method is called ) and another ( passed as an argument ).
- default IntPredicate<T> or ( IntPredicate <? super T > other ) :- It returns an intpredicate that represents short circuiting logical OR of this intpredicate ( intpredicate on which method is called ) and another ( passed as an argument ).
- default IntPredicate<T> negate ( ) :- It returns an intpredicate that represents logical negation of this intpredicate ( intpredicate on which method is called ).
Reference :- IntPredicate Interface JavaDocs
Example – 1 :-
This example will show you, how to create and call different methods of a intpredicate interface.
public static void main(String[] args){ IntPredicate intPredicate1 = ( x ) -> { if( x == 8 ){ return true; }else{ return false; }}; // Example of test method boolean result = intPredicate1.test(8); System.out.println(result); // Example of negate method boolean result3 = (intPredicate1.negate()).test(8); System.out.println(result3); IntPredicate intPredicate2 = ( x ) -> { if( x == 9 ){ return true; }else{ return false; }}; // Example of and method boolean result1 = intPredicate1.and(intPredicate2).test(8); System.out.println(result1); // Example of or method boolean result2 = intPredicate1.or(intPredicate2).test(8); System.out.println(result2); }
Result :-
true false false true
That’s all for Java 8 IntPredicate Interface. If you liked it, please share your thoughts in comments section and share it with others too.