Saturday, July 25, 2015

Java lambda - LongPredicate Functional Interface

LongPredicate Functional Interface

Previously we have discussed Predicate Interface and all its methods with examples. I would highly recommend that you read about that first before proceeding to read about LongPredicate. Predicate Interface can accept any type but LongPredicate can only accept input of long primitive type only.

LongPredicate is a long valued predicate which deals with single boolean argument. So LongPredicate is a specialized version (only for long argument) of Predicate interface.



LongPredicate Interface Declaration

public interface LongPredicate
LongPredicate interface only accepts long argument



test() method

boolean test(long value);
test method is used to evaluate this predicate on given argument.

The below code returns true if parameter is greater than 10 else returns false.

LongPredicate longPred = (x) -> x > 10;
System.out.println(longPred.test(11)); // Outputs true
System.out.println(longPred.test(9)); // Outputs false



and() method

default LongPredicate and(LongPredicate other) {
    Objects.requireNonNull(other);
    return (value) -> test(value) && other.test(value);
}

and() returns the composed version of two different LongPredicates. Both the predicates are evaluated in sequence. The composed version represents the short-circuiting logical and of both the predicates. This method will throw NullPointerException is other predicate is null.

We will write two different predicates. One checks for number greater than 10 and another check for number less than 20. So it will return true if number is greater than 10 and less than 20.

LongPredicate longPredGreater = (x) -> x > 10;
LongPredicate longPredLesser = (x) -> x < 20;
LongPredicate combine = longPredGreater.and(longPredLesser);
System.out.println(combine.test(17)); // Outputs true
System.out.println(combine.test(25)); // Outputs false



negate() method

default LongPredicate negate() {
    return (value) -> !test(value);
}
negate() method returns the logical negation of this predicate.
Let us take simple example to demonstrate this method.

We will write LongPredicate that will return true if number is greater than 10.
LongPredicate longPredGreater = (x) -> x > 10;
System.out.println(longPredGreater.test(11)); //Outputs true
System.out.println(longPredGreater.test(9)); //Outputs false

Now we will apply negate method on this predicate.
LongPredicate negation = longPredGreater.negate();
System.out.println(negation.test(11)); //Outputs false
System.out.println(negation.test(9));  //Outputs true

The output is now reversed.



or() method

default LongPredicate or(LongPredicate other) {
    Objects.requireNonNull(other);
    return (value) -> test(value) || other.test(value);
}
or() method returns the composed predicate that represents the sort-circuiting logical OR for this predicate and another. While evaluating the composed version of predicate, if this predicate is true then the other predicate is not evaluated. This method will throw NullPointerException is other predicate is null.

We will write two different predicates. One check for number greater than 10 and less than 20 and another check for number greater than 40 and less than 50. So it will return true if number is greater than 10 and less than 20 or number is greater than 40 and less than 50.

LongPredicate longPred1 = (x) -> x > 10 && x < 20;
LongPredicate longPred2 = (x) -> x > 40 && x < 50;
           
LongPredicate or = longPred1.or(longPred2);
System.out.println(or.test(15)); //Outputs true. Number is between 10 and 20
System.out.println(or.test(47)); //Outputs true. Number is between 40 and 50
System.out.println(or.test(7)); //Outputs false. Number < 10 and Number < 40
System.out.println(or.test(32)); //Outputs false. Number > 20 and Number < 40


That’s all on LongPredicate Interface

Read about important java.util.function package’s interface here. Consumer, Function, Supplier, BinaryOperator & Predicate Functional Interfaces. I have also written on High Order functions using Function functional interface.



No comments:

Post a Comment

Ads Inside Post