LongPredicate Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expect one long-valued argument as an input and produces a boolean value as an output. As it is a functional interface, it can be used assignment target for lambda expression or method reference. In this post, we are going to see several implementations of LongPredicate Interface by using different examples.
LongPredicate Interface in Java 8 with Examples...!!! Click To Tweet
Look at LongPredicate Javadoc description below:
LongPredicate Interface contains 4 methods:
test
and
or
negate
Let’s discuss these methods:
test
This method evaluates predicate on the given long-valued argument and produces a boolean result.
1 | boolean test(long value); |
and
This method returns a composed predicate that represents a short-circuiting logical AND of this predicate and another. When evaluating the composed predicate, if this predicate is false, then the other predicate is not evaluated.
1 2 3 4 | default LongPredicate and(LongPredicate other) { Objects.requireNonNull(other); return (value) -> test(value) && other.test(value); } |
or
This method returns a composed predicate that represents a short-circuiting logical OR of this predicate and another. When evaluating the composed predicate, if this predicate is true, then the other predicate is not evaluated.
1 2 3 4 | default LongPredicate or(LongPredicate other) { Objects.requireNonNull(other); return (value) -> test(value) || other.test(value); } |
negate
This method returns a predicate that represents the logical negation of this predicate.
1 2 3 | default LongPredicate negate() { return (value) -> !test(value); } |
Lets understand above mentioned methods through various examples:
Example 1. ‘test’ method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.function.LongPredicate; public class LongPredicateInterfaceJava8Example1 { public static void main(String[] args) { System.out.println("LongPredicate Interface - Example 1"); //Lambda implementation LongPredicate longPredicateObj1 = i -> (long) i == -10; //Anonymous Class implementation LongPredicate longPredicateObj2 = new LongPredicate() { @Override public boolean test(long value) { return value > 100; } }; System.out.println("Result Obj 1: " + longPredicateObj1.test(10)); System.out.println("Result Obj 2: " + longPredicateObj2.test(5000)); } } |
Example 2. ‘and’, ‘or’ and ‘negate’ methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.function.LongPredicate; public class LongPredicateInterfaceJava8Example2 { public static void main(String[] args) { System.out.println("LongPredicate Interface - Example 2"); LongPredicate longPredicateObj1 = i - > i >= 10; LongPredicate longPredicateObj2 = i - > i == 5; LongPredicate longPredicateObj3 = i - > i < 70; //and operation System.out.println("Result 1: " + longPredicateObj1.and(longPredicateObj2).test(15)); //or operation System.out.println("Result 2: " + longPredicateObj1.or(longPredicateObj2).or(longPredicateObj3).test(15)); //negate operation System.out.println("Result 3: " + longPredicateObj3.negate().test(10)); } } |
LongPredicate Interface in Java 8 with Examples...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Supplier Interface in Java 8 with Examples