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