Java ObjDoubleConsumer

Introduction

In Java, the ObjDoubleConsumer interface is a functional interface that represents an operation that accepts an object and a double-valued argument and returns no result. It is part of the java.util.function package and is commonly used for operations that involve both an object and a double value, such as modifying an object's state.

Table of Contents

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

1. What is ObjDoubleConsumer?

ObjDoubleConsumer is a functional interface that performs an operation on an object and a double value without returning any result. It is useful for scenarios where an object's state needs to be modified based on a double input.

2. Methods and Syntax

The main method in the ObjDoubleConsumer interface is:

  • void accept(T t, double value): Performs this operation on the given object and double argument.

Syntax

ObjDoubleConsumer<T> objDoubleConsumer = (T t, double value) -> {
    // operation on t and value
};

3. Examples of ObjDoubleConsumer

Example 1: Updating Account Balance

import java.util.function.ObjDoubleConsumer;

class Account {
    private String name;
    private double balance;

    public Account(String name, double balance) {
        this.name = name;
        this.balance = balance;
    }

    public void deposit(double amount) {
        this.balance += amount;
    }

    @Override
    public String toString() {
        return name + "'s balance: " + balance;
    }
}

public class AccountUpdateExample {
    public static void main(String[] args) {
        Account account = new Account("John", 100.0);

        // Define an ObjDoubleConsumer that deposits a double value to an account
        ObjDoubleConsumer<Account> deposit = (acc, amount) -> acc.deposit(amount);

        deposit.accept(account, 50.0);
        System.out.println(account);
    }
}

Output:

John's balance: 150.0

Example 2: Applying a Discount to a Product

import java.util.function.ObjDoubleConsumer;

class Product {
    private String name;
    private double price;

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public void applyDiscount(double discount) {
        this.price -= this.price * discount / 100;
    }

    @Override
    public String toString() {
        return name + "'s price after discount: " + price;
    }
}

public class ProductDiscountExample {
    public static void main(String[] args) {
        Product product = new Product("Laptop", 1000.0);

        // Define an ObjDoubleConsumer that applies a discount to a product
        ObjDoubleConsumer<Product> applyDiscount = (prod, discount) -> prod.applyDiscount(discount);

        applyDiscount.accept(product, 10.0);
        System.out.println(product);
    }
}

Output:

Laptop's price after discount: 900.0

4. Real-World Use Case: Logging Temperature Readings

In applications, ObjDoubleConsumer can be used to log temperature readings to a sensor object.

import java.util.function.ObjDoubleConsumer;

class Sensor {
    private String id;

    public Sensor(String id) {
        this.id = id;
    }

    public void logTemperature(double temperature) {
        System.out.println("Sensor " + id + " recorded temperature: " + temperature);
    }
}

public class TemperatureLoggingExample {
    public static void main(String[] args) {
        Sensor sensor = new Sensor("A1");

        // Define an ObjDoubleConsumer to log temperature readings
        ObjDoubleConsumer<Sensor> logTemperature = (sens, temp) -> sens.logTemperature(temp);

        logTemperature.accept(sensor, 25.5);
    }
}

Output:

Sensor A1 recorded temperature: 25.5

Conclusion

The ObjDoubleConsumer interface is a versatile tool in Java for performing operations on an object and a double value without returning a result. It simplifies handling tasks like updating object states or logging information. Using ObjDoubleConsumer can lead to cleaner and more efficient code, especially in functional programming contexts.

Comments