Java LongPredicate

Introduction

In Java, the LongPredicate interface is a functional interface that represents a predicate (boolean-valued function) with a single long-valued argument. It is part of the java.util.function package and is used for testing conditions involving long values.

Table of Contents

  1. What is LongPredicate?
  2. Methods and Syntax
  3. Examples of LongPredicate
  4. Real-World Use Case
  5. Conclusion

1. What is LongPredicate?

LongPredicate is a functional interface that accepts a long and returns a boolean result. It is commonly used for evaluating conditions or filtering data.

2. Methods and Syntax

The main methods in the LongPredicate interface are:

  • boolean test(long value): Evaluates this predicate on the given argument.
  • default LongPredicate and(LongPredicate other): Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
  • default LongPredicate or(LongPredicate other): Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
  • default LongPredicate negate(): Returns a predicate that represents the logical negation of this predicate.

Syntax

LongPredicate longPredicate = (long value) -> {
    // condition on value
    return result;
};

3. Examples of LongPredicate

Example 1: Checking if a Number is Even

import java.util.function.LongPredicate;

public class EvenCheckExample {
    public static void main(String[] args) {
        // Define a LongPredicate that checks if a number is even
        LongPredicate isEven = (value) -> value % 2 == 0;

        boolean result = isEven.test(10L);
        System.out.println("Is 10 even? " + result);
    }
}

Output:

Is 10 even? true

Example 2: Using and

import java.util.function.LongPredicate;

public class AndExample {
    public static void main(String[] args) {
        // Define predicates for checking if a number is even and greater than 5
        LongPredicate isEven = (value) -> value % 2 == 0;
        LongPredicate isGreaterThanFive = (value) -> value > 5;

        LongPredicate isEvenAndGreaterThanFive = isEven.and(isGreaterThanFive);

        boolean result = isEvenAndGreaterThanFive.test(8L);
        System.out.println("Is 8 even and greater than 5? " + result);
    }
}

Output:

Is 8 even and greater than 5? true

Example 3: Using or

import java.util.function.LongPredicate;

public class OrExample {
    public static void main(String[] args) {
        // Define predicates for checking if a number is even or greater than 5
        LongPredicate isEven = (value) -> value % 2 == 0;
        LongPredicate isGreaterThanFive = (value) -> value > 5;

        LongPredicate isEvenOrGreaterThanFive = isEven.or(isGreaterThanFive);

        boolean result = isEvenOrGreaterThanFive.test(3L);
        System.out.println("Is 3 even or greater than 5? " + result);
    }
}

Output:

Is 3 even or greater than 5? false

Example 4: Using negate

import java.util.function.LongPredicate;

public class NegateExample {
    public static void main(String[] args) {
        // Define a predicate for checking if a number is even
        LongPredicate isEven = (value) -> value % 2 == 0;

        LongPredicate isOdd = isEven.negate();

        boolean result = isOdd.test(5L);
        System.out.println("Is 5 odd? " + result);
    }
}

Output:

Is 5 odd? true

4. Real-World Use Case: Filtering Even Numbers from a List

In applications, LongPredicate can be used to filter even numbers from a list.

import java.util.function.LongPredicate;
import java.util.stream.LongStream;

public class FilterEvenNumbers {
    public static void main(String[] args) {
        // Define a LongPredicate to check for even numbers
        LongPredicate isEven = (value) -> value % 2 == 0;

        LongStream.of(1L, 2L, 3L, 4L, 5L, 6L)
                .filter(isEven)
                .forEach(value -> System.out.println("Even number: " + value));
    }
}

Output:

Even number: 2
Even number: 4
Even number: 6

Conclusion

The LongPredicate interface is used in Java for evaluating conditions involving long values. It simplifies filtering and logical operations, enhancing code clarity and maintainability. Using LongPredicate can lead to cleaner and more efficient code, especially in functional programming contexts.

Comments