How do I use the ObjLongConsumer functional interface in Java?
The ObjLongConsumer functional interface in Java is part of the java.util.function package and is available since Java 8. It represents an operation that accepts two input arguments: an object T and a long-valued argument, and performs some operation without returning any result. It is effectively a BiConsumer specialized for one object and one long argument. […]
Artikel oleh Wayan · Sumber: Kode Java ↗
Terjemahan artikel ini belum tersedia. Isi asli ditampilkan.
The ObjLongConsumer functional interface in Java is part of the java.util.function package and is available since Java 8. It represents an operation that accepts two input arguments: an object T and a long-valued argument, and performs some operation without returning any result. It is effectively a BiConsumer specialized for one object and one long argument.
Here’s a breakdown of how it works and how to use it:
Key Characteristics:
- Functional Interface:
- It is annotated with
@FunctionalInterface, meaning it contains a single abstract method to implement. - The method signature is:
- It is annotated with
void accept(T t, long value);
- Input:
- A generic object
T(the first parameter). - A
longvalue (the second parameter).
- A generic object
- Output:
- It does not return any value (similar to
Consumer).
- It does not return any value (similar to
- Use Case:
- Typically used in scenarios where we want to consume or process an object and a numeric value (e.g., processing an object with an associated count, index, or timestamp).
Example Usage
Here’s how we can use the ObjLongConsumer interface effectively:
package org.kodejava.util.function;
import java.util.function.ObjLongConsumer;
public class ObjLongConsumerExample {
public static void main(String[] args) {
// Create an ObjLongConsumer to print an object and a long value
ObjLongConsumer<String> printDetails = (str, num) -> {
System.out.println("String: " + str + ", Number: " + num);
};
// Use the ObjLongConsumer
printDetails.accept("Example", 50);
}
}
Output:
String: Example, Number: 50
Another Example with a Data Processor
Let’s see a more practical use case, for example, processing an object (like a product) with an associated long value (like its stock count):
package org.kodejava.util.function;
import java.util.function.ObjLongConsumer;
public class ObjLongConsumerExample2 {
public static void main(String[] args) {
ObjLongConsumer<Item> updateStock = (item, stock) -> {
System.out.println("Item: " + item.name + ", Stock: " + stock);
};
Item apple = new Item("Apple");
Item banana = new Item("Banana");
// Update the stock of products
updateStock.accept(apple, 100L);
updateStock.accept(banana, 50L);
}
}
class Item {
String name;
public Item(String name) {
this.name = name;
}
}
Output:
Item: Apple, Stock: 100
Item: Banana, Stock: 50
Real-World Use Cases
- Logging operations:
- Log details of an event and a timestamp.
- Streams and Iteration:
- It can be used in conjunction with streams where we need to process both an object and a primitive value like a
long.
- It can be used in conjunction with streams where we need to process both an object and a primitive value like a
For example, using a loop with an index:
package org.kodejava.util.function;
import java.util.function.ObjLongConsumer;
import java.util.stream.IntStream;
public class ObjLongConsumerWithStreams {
public static void main(String[] args) {
ObjLongConsumer<String> indexedPrinter = (value, index) -> {
System.out.println("Index: " + index + ", Value: " + value);
};
// Example: Using a range with an array
String[] items = {"Apple", "Banana", "Cherry"};
IntStream.range(0, items.length).forEach(i -> indexedPrinter.accept(items[i], i));
}
}
Output:
Index: 0, Value: Apple
Index: 1, Value: Banana
Index: 2, Value: Cherry
Summary
- Use
ObjLongConsumerwhen we need a functional interface that processes a combination of an object and alongvalue without returning a result. - It is particularly useful for processing lists, objects with associated numeric properties, or items in streams with their indices.
- The
acceptmethod is where we specify what happens with the inputs.
This makes ObjLongConsumer a simple yet powerful tool for functional programming in Java!
