How do I use the ObjDoubleConsumer functional interface in Java?

In Java, the ObjDoubleConsumer is part of the java.util.function package, and it represents a functional interface. It takes two arguments:

  1. An object of type T
  2. A double-valued argument

The functional method of this interface is accept(T t, double value), which performs an operation that accepts these two parameters but does not return any result (void).

Steps to Use ObjDoubleConsumer

  1. Use as a Lambda Expression: We can implement the accept method using a lambda.
  2. Use for Side Effects: This interface is typically used for operations that perform side effects rather than computing a result (e.g., logging, modifying an object, etc.).

Example 1: Using ObjDoubleConsumer with a Lambda

Here’s a basic example that demonstrates logging an object and a double value.

package org.kodejava.util.function;

import java.util.function.ObjDoubleConsumer;

public class ObjDoubleConsumerExample {
    public static void main(String[] args) {
        ObjDoubleConsumer<String> logger = (str, value) -> {
            System.out.println("The provided string: " + str);
            System.out.println("The associated double value: " + value);
        };

        // Using the ObjDoubleConsumer
        logger.accept("Temperature", 36.5);
    }
}

Output:

The provided string: Temperature
The associated double value: 36.5

Example 2: Modifying an Object Using ObjDoubleConsumer

Here’s an example of modifying an object field using an ObjDoubleConsumer.

package org.kodejava.util.function;

import java.util.function.ObjDoubleConsumer;

public class ObjDoubleConsumerExample2 {
    public static void main(String[] args) {
        ObjDoubleConsumer<Box> updateWeight = (box, weight) -> box.weight = weight;

        // Create a Box object
        Box box = new Box("Package1", 5.0);
        System.out.println("Before: " + box);

        // Update the weight using ObjDoubleConsumer
        updateWeight.accept(box, 10.5);
        System.out.println("After: " + box);
    }
}

class Box {
    String label;
    double weight;

    public Box(String label, double weight) {
        this.label = label;
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Box[label=" + label + ", weight=" + weight + "]";
    }
}

Output:

Before: Box[label=Package1, weight=5.0]
After: Box[label=Package1, weight=10.5]

Example 3: Using ObjDoubleConsumer with Streams

Sometimes, ObjDoubleConsumer works well with streams. For instance, it is useful when working with an operation based on an object and a primitive value (such as logging or computations).

package org.kodejava.util.function;

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

public class ObjDoubleConsumerStreamExample3 {
    public static void main(String[] args) {
        DoubleStream doubleStream = DoubleStream.of(1.5, 2.7, 3.8);

        // ObjDoubleConsumer to print values with a prefix
        ObjDoubleConsumer<String> printer =
                (prefix, value) -> System.out.println(prefix + ": " + value);

        // Use it with a stream
        doubleStream.forEach(value -> printer.accept("Value", value));
    }
}

Output:

Value: 1.5
Value: 2.7
Value: 3.8

Key Points:

  • Functional Method: accept(T t, double value) is the functional method.
  • Target Use: Designed for operations that take two arguments (T and double) and produce side effects.
  • Common Usage: Modifying existing objects, logging two parameters, or iterating over collections of objects and associated double values.

By following these use cases, we can effectively incorporate ObjDoubleConsumer wherever applicable!

Leave a Reply

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