Introduction
In Java, the ObjLongConsumer
interface is a functional interface that represents an operation that accepts an object and a long
-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 long
value, such as modifying an object’s state.
Table of Contents
- What is
ObjLongConsumer
? - Methods and Syntax
- Examples of
ObjLongConsumer
- Real-World Use Case
- Conclusion
1. What is ObjLongConsumer?
ObjLongConsumer
is a functional interface that performs an operation on an object and a long
value without returning any result. It is useful for scenarios where an object’s state needs to be modified based on a long
input.
2. Methods and Syntax
The main method in the ObjLongConsumer
interface is:
void accept(T t, long value)
: Performs this operation on the given object andlong
argument.
Syntax
ObjLongConsumer<T> objLongConsumer = (T t, long value) -> { // operation on t and value };
3. Examples of ObjLongConsumer
Example 1: Updating Account Balance
import java.util.function.ObjLongConsumer; class Account { private String name; private long balance; public Account(String name, long balance) { this.name = name; this.balance = balance; } public void deposit(long 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", 100L); // Define an ObjLongConsumer that deposits a long value to an account ObjLongConsumer<Account> deposit = (acc, amount) -> acc.deposit(amount); deposit.accept(account, 50L); System.out.println(account); } }
Output:
John's balance: 150
Example 2: Adding Quantity to a Product
import java.util.function.ObjLongConsumer; class Product { private String name; private long quantity; public Product(String name, long quantity) { this.name = name; this.quantity = quantity; } public void addQuantity(long 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", 10L); // Define an ObjLongConsumer that adds quantity to a product ObjLongConsumer<Product> addQuantity = (prod, amount) -> prod.addQuantity(amount); addQuantity.accept(product, 5L); System.out.println(product); } }
Output:
Laptop's quantity after addition: 15
4. Real-World Use Case: Logging Timestamps
In applications, ObjLongConsumer
can be used to log timestamps to a logger object.
import java.util.function.ObjLongConsumer; class Logger { private String id; public Logger(String id) { this.id = id; } public void logTimestamp(long timestamp) { System.out.println("Logger " + id + " recorded timestamp: " + timestamp); } } public class TimestampLoggingExample { public static void main(String[] args) { Logger logger = new Logger("Logger1"); // Define an ObjLongConsumer to log timestamps ObjLongConsumer<Logger> logTimestamp = (log, time) -> log.logTimestamp(time); logTimestamp.accept(logger, System.currentTimeMillis()); } }
Output:
Logger Logger1 recorded timestamp: [current timestamp in milliseconds]
Conclusion
The ObjLongConsumer
interface is a versatile tool in Java for performing operations on an object and a long
value without returning a result. It simplifies handling tasks like updating object states or logging information. Using ObjLongConsumer
can lead to cleaner and more efficient code, especially in functional programming contexts.