How do I use the DoublePredicate functional interface in Java?

The DoublePredicate interface in Java, part of the java.util.function package, is a functional interface used to represent a predicate (boolean-valued function) that tests a single double-valued argument.

Functional Interface Details

The DoublePredicate interface has only one abstract method:

boolean test(double value);

This method evaluates the predicate on the given double value and returns a boolean result.

How to Use DoublePredicate

1. Using a Lambda Expression

We can use a lambda expression to define the behavior of the DoublePredicate. For example, to check if a double value is greater than a certain threshold:

package org.kodejava.util.function;

import java.util.function.DoublePredicate;

public class DoublePredicateExample {
    public static void main(String[] args) {
        // Define a DoublePredicate to check if a value is greater than 10.0
        DoublePredicate isGreaterThanTen = value -> value > 10.0;

        // Test the predicate
        // Output: true
        System.out.println(isGreaterThanTen.test(15.5));
        // Output: false
        System.out.println(isGreaterThanTen.test(8.2));
    }
}

2. Using the and, or, and negate Methods

DoublePredicate provides built-in methods for combining predicates:
and(DoublePredicate other): Combines the current predicate with another predicate using a logical AND.
or(DoublePredicate other): Combines the current predicate with another predicate using a logical OR.
negate(): Returns a predicate that represents the logical negation of the current predicate.

Example:

package org.kodejava.util.function;

import java.util.function.DoublePredicate;

public class DoublePredicateCombination {
    public static void main(String[] args) {
        // Define basic predicates
        DoublePredicate isPositive = value -> value > 0;
        DoublePredicate isLessThanTen = value -> value < 10;

        // Combine predicates
        DoublePredicate isPositiveAndLessThanTen = isPositive.and(isLessThanTen);
        DoublePredicate isNegativeOrZero = isPositive.negate();

        // Test the combined predicates
        // Output: true
        System.out.println(isPositiveAndLessThanTen.test(5));
        // Output: false
        System.out.println(isPositiveAndLessThanTen.test(15));
        // Output: true
        System.out.println(isNegativeOrZero.test(-3));
    }
}

3. Using DoublePredicate with Streams

DoublePredicate works seamlessly with Java’s DoubleStream API for filtering primitive stream elements:

package org.kodejava.util.function;

import java.util.function.DoublePredicate;
import java.util.stream.DoubleStream;

public class DoublePredicateWithStream {
    public static void main(String[] args) {
        // Create a DoubleStream
        DoubleStream doubleStream = DoubleStream.of(1.5, -2.0, 3.8, 5.0, -1.1);

        // Define a predicate to filter positive values
        DoublePredicate isPositive = value -> value > 0;

        // Filter and print positive values
        doubleStream.filter(isPositive)
                .forEach(value -> System.out.println("Positive value: " + value));
    }
}

Output:

Positive value: 1.5
Positive value: 3.8
Positive value: 5.0

Key Points

  1. Implementation: DoublePredicate is a functional interface, so it can be implemented using lambda expressions, method references, or anonymous inner classes.
  2. Predicate Composition: Use and, or, and negate methods to create complex predicates.
  3. Integration in Streams: Useful for filtering double values in DoubleStream.

Leave a Reply

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