What is the LongPredicate functional interface in Java?

LongPredicate is a functional interface which accepts a long valued argument and returns a boolean value. This interface has the following methods:

  1. test(long value)
  2. and(LongPredicate other)
  3. negate()
  4. or(LongPredicate other)

The LongPredicate interface is defined in the java.util.function package. To import the LongPredicate interface, we will check the following import statement:

import java.util.function.LongPredicate;

What is test(long value)?

The test(long value) method is used to test a given long valued input on certain evaluations.

Syntax


boolean test(long value)


Parameters

  • long value: This is the long valued input that needs to be evaluated.

Return value

This method returns the result of the evaluation.

Code

import java.util.function.LongPredicate;
public class Main{
public static void main(String[] args) {
// LongPredicate to check if the long valued argument is even
LongPredicate isEven = arg -> arg % 2 == 0;
// test whether 4 is even
System.out.println(isEven.test(4));
// test whether 3 is even
System.out.println(isEven.test(3));
}
}

What is and(LongPredicate other)?

The and(LongPredicate other) method is used to chain multiple implementations of the LongPredicate together as a short-circuiting logical AND operation. If any implementation returns False during the evaluation, then the subsequent implementations will not be invoked.

Syntax


default LongPredicate and(LongPredicate other)

Parameters

  • LongPredicate other: This is the LongPredicate implementation that’s used for logical AND operation.

Return value

This method returns a composed predicate.

Code

import java.util.function.LongPredicate;;
public class Main{
public static void main(String[] args) {
// LongPredicate to check if the long valued argument is even
LongPredicate isEven = arg -> arg % 2 == 0;
// LongPredicate to check if the long valued argument is less than 100
LongPredicate isLessThanHundred = arg -> arg < 100;
// Composed LongPredicate
LongPredicate composedPredicate = isEven.and(isLessThanHundred);
// test whether 4 is even and less than 100
System.out.println(composedPredicate.test(4));
// test whether 103 is even and less than 100
System.out.println(composedPredicate.test(103));
}
}

What is negate()?

The negate() method is used to return a LongPredicate that is a negation of the given LongPredicate.

Syntax


default LongPredicate negate()

Parameters

This method has no parameters.

Return value

This method returns a predicate.

Code

import java.util.function.LongPredicate;
public class Main{
public static void main(String[] args) {
// LongPredicate to check if the long valued argument is even
LongPredicate isEven = arg -> arg % 2 == 0;
// LongPredicate to get the Opposite of even using the negate method
LongPredicate isEvenNegate = isEven.negate();
// test negation on 4
System.out.println(isEvenNegate.test(4));
// test negation on 3
System.out.println(isEvenNegate.test(3));
}
}

What is or(LongPredicate other)?

The or(LongPredicate other) method is used to chain multiple implementations of LongPredicate together as a short-circuiting logical OR operation. If any implementation returns True during the evaluation, then the subsequent implementations will not be invoked.

Syntax


default LongPredicate or(LongPredicate other)

Parameters

  • LongPredicate other: This is the LongPredicate implementation that’s used for logical OR operation.

Return value

This method returns a composed predicate.

Code

import java.util.function.LongPredicate;
public class Main{
public static void main(String[] args) {
// LongPredicate to check if the long valued argument is even
LongPredicate isEven = arg -> arg % 2 == 0;
// LongPredicate to check if the long valued argument is less than 100
LongPredicate isLessThanHundred = arg -> arg < 100;
// Composed LongPredicate
LongPredicate composedPredicate = isEven.or(isLessThanHundred);
// test whether 4 is even and less than 100
System.out.println(composedPredicate.test(4));
// test whether 103 is even and less than 100
System.out.println(composedPredicate.test(103));
}
}

Free Resources