How do I use the DoubleConsumer functional interface in Java?

The DoubleConsumer interface in Java is part of the java.util.function package and is used to represent an operation that takes a single double-valued argument and does not return a result. It is commonly used in lambda expressions or method references for consuming a double value (often for side effect operations such as logging or accumulating values).

Functional Interface

The DoubleConsumer interface is a functional interface and is annotated with @FunctionalInterface, meaning it has exactly one abstract method:

void accept(double value);

This method is applied to perform an operation using the given double value.

How to Use DoubleConsumer

Here are some scenarios where we can use DoubleConsumer:

1. Using a Lambda Expression

A common use of DoubleConsumer is to define its functionality using a lambda expression.

Example:

package org.kodejava.util.function;

import java.util.function.DoubleConsumer;

public class DoubleConsumerExample {
    public static void main(String[] args) {
        // Define a DoubleConsumer to print a double value
        DoubleConsumer printConsumer = value -> System.out.println("Value: " + value);

        // Consume a value
        printConsumer.accept(42.5);
    }
}

Output:

Value: 42.5

2. Using a Method Reference

If we have a method compatible with the accept(double) signature, you can use it as a method reference to implement DoubleConsumer.

Example:

package org.kodejava.util.function;

import java.util.function.DoubleConsumer;

public class DoubleConsumerMethodReference {
    public static void printValue(double value) {
        System.out.println("Received: " + value);
    }

    public static void main(String[] args) {
        // Use a method reference to implement DoubleConsumer
        DoubleConsumer printConsumer = DoubleConsumerMethodReference::printValue;

        // Consume a value
        printConsumer.accept(19.99);
    }
}

Output:

Received: 19.99

3. Using andThen() Method for Chaining Operations

The DoubleConsumer interface has a built-in andThen method that allows chaining multiple operations. The andThen method returns a composed DoubleConsumer that performs this operation, followed by another DoubleConsumer.

Example:

package org.kodejava.util.function;

import java.util.function.DoubleConsumer;

public class DoubleConsumerChaining {
    public static void main(String[] args) {
        // Consumer to log a value
        DoubleConsumer logConsumer = value -> System.out.println("Logging value: " + value);

        // Consumer to process a value (e.g., multiply it by 2)
        DoubleConsumer processConsumer = value -> System.out.println("Processed value: " + (value * 2));

        // Chain both consumers using andThen()
        DoubleConsumer chainedConsumer = logConsumer.andThen(processConsumer);

        // Consume a value
        chainedConsumer.accept(8.5);
    }
}

Output:

Logging value: 8.5
Processed value: 17.0

4. Using DoubleConsumer in Streams

The DoubleConsumer is often used in conjunction with Java’s Stream API, particularly when working with primitive streams like DoubleStream.

Example:

package org.kodejava.util.function;

import java.util.stream.DoubleStream;

public class DoubleConsumerWithStream {
    public static void main(String[] args) {
        // Create a DoubleStream
        DoubleStream doubleStream = DoubleStream.of(1.0, 2.5, 3.8);

        // Use DoubleConsumer to print each value in the stream
        doubleStream.forEach(value -> System.out.println("Stream value: " + value));
    }
}

Output:

Stream value: 1.0
Stream value: 2.5
Stream value: 3.8

Key Points

  1. Functional Interface: Since it is a functional interface, we can easily implement it using lambda expressions or method references.
  2. Purpose: It is ideal for consuming double values where no result is needed, such as in logging, accumulation, or side effect operations.
  3. Stream Integration: Frequently used in primitive streams (DoubleStream) for operations on elements.
  4. Method Chaining: The andThen method allows combining multiple DoubleConsumer instances into a single operation.

By leveraging DoubleConsumer, we can perform concise and reusable operations on double values throughout our application.

Wayan

Leave a Reply

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