Java ObjIntConsumer

Introduction

In Java, the ObjIntConsumer interface is a functional interface that represents an operation that accepts an object and an int-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 an int value, such as modifying an object's state.

Table of Contents

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

1. What is ObjIntConsumer?

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

2. Methods and Syntax

The main method in the ObjIntConsumer interface is:

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

Syntax

ObjIntConsumer<T> objIntConsumer = (T t, int value) -> {
    // operation on t and value
};

3. Examples of ObjIntConsumer

Example 1: Updating Account Balance

import java.util.function.ObjIntConsumer;

class Account {
    private String name;
    private int balance;

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

    public void deposit(int 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);

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

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

Output:

John's balance: 150

Example 2: Adding Quantity to a Product

import java.util.function.ObjIntConsumer;

class Product {
    private String name;
    private int quantity;

    public Product(String name, int quantity) {
        this.name = name;
        this.quantity = quantity;
    }

    public void addQuantity(int amount) {
        this.quantity += amount;
    }

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

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

        // Define an ObjIntConsumer that adds quantity to a product
        ObjIntConsumer<Product> addQuantity = (prod, amount) -> prod.addQuantity(amount);

        addQuantity.accept(product, 5);
        System.out.println(product);
    }
}

Output:

Laptop's quantity after addition: 15

4. Real-World Use Case: Logging Error Codes

In applications, ObjIntConsumer can be used to log error codes to a logger object.

import java.util.function.ObjIntConsumer;

class Logger {
    private String id;

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

    public void logError(int errorCode) {
        System.out.println("Logger " + id + " recorded error code: " + errorCode);
    }
}

public class ErrorLoggingExample {
    public static void main(String[] args) {
        Logger logger = new Logger("Logger1");

        // Define an ObjIntConsumer to log error codes
        ObjIntConsumer<Logger> logError = (log, code) -> log.logError(code);

        logError.accept(logger, 404);
    }
}

Output:

Logger Logger1 recorded error code: 404

Conclusion

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

Comments