How do I use the LongPredicate functional interface in Java?

In Java, the LongPredicate is a functional interface introduced in Java 8 as part of the java.util.function package. It represents a predicate (a boolean-valued function) that takes a single long value as its input argument.

The key method in LongPredicate is:

boolean test(long value);

Here’s how we can use the LongPredicate functional interface:

Example Usage of LongPredicate

1. Basic Example with Lambda Expression:

package org.kodejava.util.function;

import java.util.function.LongPredicate;

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

      long number = 10;

      // Use the LongPredicate
      if (isEven.test(number)) {
         System.out.println(number + " is even.");
      } else {
         System.out.println(number + " is odd.");
      }
   }
}

2. Combining LongPredicates Using and, or, and negate:

package org.kodejava.util.function;

import java.util.function.LongPredicate;

public class LongPredicateExample2 {
   public static void main(String[] args) {
      // Define two LongPredicates: even and greater than 5
      LongPredicate isEven = value -> value % 2 == 0;
      LongPredicate isGreaterThanFive = value -> value > 5;

      long number = 10;

      // Combine using 'and'
      LongPredicate isEvenAndGreaterThanFive = isEven.and(isGreaterThanFive);
      System.out.println("Is " + number + " even and greater than 5? " +
                         isEvenAndGreaterThanFive.test(number)); // true

      // Combine using 'or'
      LongPredicate isEvenOrGreaterThanFive = isEven.or(isGreaterThanFive);
      System.out.println("Is " + number + " even or greater than 5? " +
                         isEvenOrGreaterThanFive.test(number)); // true

      // Negate the 'isEven' predicate
      LongPredicate isOdd = isEven.negate();
      System.out.println("Is " + number + " odd? " + isOdd.test(number)); // false
   }
}

3. Using with Streams (e.g., LongStream):

If we use the LongPredicate with streams, it can be helpful for filtering LongStream.

package org.kodejava.util.function;

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

public class LongPredicateWithStreams {
   public static void main(String[] args) {
      // Generate a stream of numbers from 1 to 10
      LongStream stream = LongStream.rangeClosed(1, 10);

      // Define a LongPredicate to filter even numbers
      LongPredicate isEven = value -> value % 2 == 0;

      // Use 'filter' with LongPredicate
      stream.filter(isEven)
              .forEach(System.out::println); // Prints 2, 4, 6, 8, 10
   }
}

4. Method Reference with LongPredicate:

We can also use method references if we have a method that matches the signature of the test method:

package org.kodejava.util.function;

import java.util.function.LongPredicate;

public class LongPredicateMethodReference {
   public static void main(String[] args) {
      LongPredicate isPositive = LongPredicateMethodReference::isPositive;

      long number = 5;
      // true
      System.out.println("Is " + number + " positive? " + isPositive.test(number));
   }

   // Method to check if a number is positive
   public static boolean isPositive(long value) {
      return value > 0;
   }
}

Key Points about LongPredicate

  • The functional method in LongPredicate is boolean test(long value). This method evaluates the predicate against the given long value and returns a boolean result.
  • It is often used in lambda expressions or method references.
  • We can combine multiple LongPredicate instances using and, or, and negate methods.

Summary

  • Use LongPredicate when working with primitive long arguments in functional programming scenarios.
  • It helps to avoid boxing overhead compared to Predicate<Long>.
  • We can define simple or complex conditions, combine predicates using and, or, and negate, and use them with streams for concise and readable code.
Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.