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.



Java Lambda - IntPredicate Functional Interface

IntPredicate 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 IntPredicate. Predicate Interface can accept any type but IntPredicate can only accept input of int primitive type only.

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



IntPredicate Interface Declaration

public interface IntPredicate
IntPredicate interface only accepts int argument



test() method

boolean test(int 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.

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



and() method

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

and() returns the composed version of two different IntPredicates. 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.

IntPredicate intPredGreater = (x) -> x > 10;
IntPredicate intPredLesser = (x) -> x < 20;
IntPredicate combine = intPredGreater.and(intPredLesser);
System.out.println(combine.test(17)); // Outputs true
System.out.println(combine.test(25)); // Outputs false



negate() method

default IntPredicate 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 a IntPredicate that will return true if number is greater than 10.
IntPredicate intPredGreater = (x) -> x > 10;
System.out.println(intPredGreater.test(11)); //Outputs true
System.out.println(intPredGreater.test(9)); //Outputs false

Now we will apply negate method on this predicate.
IntPredicate negation=intPredGreater.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 IntPredicate or(IntPredicate 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.

IntPredicate intPred1 = (x) -> x > 10 && x < 20;
IntPredicate intPred2 = (x) -> x > 40 && x < 50;
           
IntPredicate or = intPred1.or(intPred2);
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 IntPredicate 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.


Java Lambda - LongUnaryOperator Functional Interface

LongUnaryOperator Functional Interface

Before we discuss LongUnaryOperator I would like to point out that this interface is primitive type specialization of UnaryOperator. Also, UnaryOperator Interface extends Function interface. I highly recommend that you read those interfaces.

LongUnaryOperator interface represents operation on long valued operand that produces long valued result.

Below are the explanations of methods in LongUnaryOperator interface.



applyAsLong() method

long applyAsLong(long operand);

applyAsLong() method applies this operator to operand passed as parameter in method.

LongUnaryOperator adder = (val) -> val + val;
System.out.println(adder.applyAsLong(10)); //Output 20



compose() method

default LongUnaryOperator compose(LongUnaryOperator before) {
    Objects.requireNonNull(before);
    return (long v) -> applyAsLong(before.applyAsLong(v));
}

Compose method is used to return a composed operator that applies before operator (as in parameter) first and then applies this operator to the result. If the operator passed in parameter is null then this method will throw NullPointerException. Let us take simple example of addition and subtractions using LongUnaryOperator.

LongUnaryOperator substracter = (val) -> {
    val = val - 10;
    System.out.println("substracter " + val);
    return val;
};

LongUnaryOperator adder = (val) -> {
    val = val + 13;
    System.out.println("adder " + val);
    return val;
};

adder.compose(substracter).applyAsLong(33);

Output
substracter 23
adder 36



andThen() method

default LongUnaryOperator andThen(LongUnaryOperator after) {
    Objects.requireNonNull(after);
    return (long t) -> after.applyAsLong(applyAsLong(t));
}

andThen() method is used to return the composed operator that applies this operator first and then it applies after operator to the result of this operator. If the operator passed in parameter is null then this method will throw NullPointerException. Let us take simple example of addition and subtractions using LongUnaryOperator.

LongUnaryOperator substracter = (val) -> {
    val = val - 10;
    System.out.println("substracter " + val);
    return val;
};

LongUnaryOperator adder = (val) -> {
    val = val + 13;
    System.out.println("adder " + val);
    return val;
};

adder.andThen(substracter).applyAsLong(33);

Output
adder 46
substracter 36



identity() method

static LongUnaryOperator identity() {
    return t -> t;
}

identity() method returns a unary operator that will always return the input argument.

LongUnaryOperator identity = LongUnaryOperator.identity();
System.out.println(identity.applyAsLong (10));
Output
10

That’s all on LongUnaryOperator 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.





Java Lambda - IntUnaryOperator Functional Interface

IntUnaryOperator Functional Interface

Before we discuss IntUnaryOperator I would like to point out that this interface is primitive type specialization of UnaryOperator. Also, UnaryOperator Interface extends Function interface. I highly recommend that you read those interfaces.

IntUnaryOperator interface represents operation on int valued operand that produces int valued result.

Below are the explanations of methods in IntUnaryOperator interface.



applyAsInt() method

int applyAsInt(int operand);

applyAsInt() method applies this operator to operand passed as parameter in method.

IntUnaryOperator adder = (val) -> val + val;
System.out.println(adder.applyAsInt(10)); //Output 20



compose() method

default IntUnaryOperator compose(IntUnaryOperator before) {
    Objects.requireNonNull(before);
    return (int v) -> applyAsInt(before.applyAsInt(v));
}

Compose method is used to return a composed operator that applies before operator (as in parameter) first and then applies this operator to the result. If the operator passed in parameter is null then this method will throw NullPointerException. Let us take simple example of addition and subtractions using IntUnaryOperator.

IntUnaryOperator substracter = (val) -> {
    val = val - 10;
    System.out.println("substracter " + val);
    return val;
};

IntUnaryOperator adder = (val) -> {
    val = val + 13;
    System.out.println("adder " + val);
    return val;
};

adder.compose(substracter).applyAsInt(33);

Output
substracter 23
adder 36



andThen() method

default IntUnaryOperator andThen(IntUnaryOperator after) {
    Objects.requireNonNull(after);
    return (int t) -> after.applyAsInt(applyAsInt(t));
}

andThen() method is used to return the composed operator that applies this operator first and then it applies after operator to the result of this operator. If the operator passed in parameter is null then this method will throw NullPointerException. Let us take simple example of addition and subtractions using IntUnaryOperator.

IntUnaryOperator substracter = (val) -> {
    val = val - 10;
    System.out.println("substracter " + val);
    return val;
};

IntUnaryOperator adder = (val) -> {
    val = val + 13;
    System.out.println("adder " + val);
    return val;
};

adder.andThen(substracter).applyAsInt(33);

Output
adder 46
substracter 36



identity() method

static IntUnaryOperator identity() {
    return t -> t;
}

identity() method returns a unary operator that will always return the input argument.

IntUnaryOperator identity = IntUnaryOperator.identity();
System.out.println(identity.applyAsInt(10));
Output
10

That’s all on IntUnaryOperator 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.




Java Lambda - DoubleUnaryOperator Functional Interface

DoubleUnaryOperator Functional Interface

Before we discuss DoubleUnaryOperator I would like to point out that this interface is primitive type specialization of UnaryOperator. Also, UnaryOperator Interface extends Function interface. I highly recommend that you read those interfaces.

DoubleUnaryOperator interface represents operation on double valued operand that produces double valued result.

Below are the explanations of methods in DoubleUnaryOperator interface.



applyAsDouble() method

double applyAsDouble(double operand);

applyAsDouble() method applies this operator to operand passed as parameter in method.
DoubleUnaryOperator power = (val) -> Math.pow(val, 2);
System.out.println(power.applyAsDouble(10.0)); //Outputs 100.0



compose() method

default DoubleUnaryOperator compose(DoubleUnaryOperator before) {
    Objects.requireNonNull(before);
    return (double v) -> applyAsDouble(before.applyAsDouble(v));
}

Compose method is used to return a composed operator that applies before operator (as in parameter) first and then applies this operator to the result. If the operator passed in parameter is null then this method will throw NullPointerException. Let us take simple example of addition and subtractions using DoubleUnaryOperator.

DoubleUnaryOperator subtracter = (val) -> {
    val = val - 10.9;
    System.out.println("subtracter " + val);
    return val;
};

DoubleUnaryOperator adder = (val) -> {
    val = val + 10.5;
    System.out.println("adder " + val);
    return val;
};

adder.compose(subtracter).applyAsDouble(33);

Output
subtracter 22.1
adder 32.6



andThen() method

default DoubleUnaryOperator andThen(DoubleUnaryOperator after) {
    Objects.requireNonNull(after);
    return (double t) -> after.applyAsDouble(applyAsDouble(t));
}

andThen() method is used to return the composed operator that applies this operator first and then it applies after operator to the result of this operator. If the operator passed in parameter is null then this method will throw NullPointerException. Let us take simple example of addition and subtractions using DoubleUnaryOperator.

DoubleUnaryOperator substracter = (val) -> {
    val = val - 10.9;
    System.out.println("substracter " + val);
    return val;
};

DoubleUnaryOperator adder = (val) -> {
    val = val + 10.5;
    System.out.println("adder " + val);
    return val;
};

adder.andThen(substracter).applyAsDouble(33);

Output
adder 43.5
substracter 32.6



identity() method

static DoubleUnaryOperator identity() {
    return t -> t;
}

identity() method returns a unary operator that will always return the input argument.

DoubleUnaryOperator identity=DoubleUnaryOperator.identity();
System.out.println(identity.applyAsDouble(10));

Output
10         


That’s all on DoubleUnaryOperator 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.



Ads Inside Post