The ObjLongConsumer interface is a functional interface from the java.util.function package, introduced in Java 8. It represents an operation that accepts two arguments: one object and one long value, without returning a result.
- The first parameter is a generic reference type T, while the second parameter is a primitive long.
- Its functional method is accept().
Method of ObjLongConsumer
The ObjLongConsumer interface provides a single abstract method, accept(), for performing an operation with the supplied arguments.
accept()
The accept() method receives an object and a long value and executes the defined operation without returning a result.
Syntax:
void accept(T t, long value)
import java.util.Arrays;
import java.util.List;
import java.util.function.ObjLongConsumer;
public class GFG {
public static void main(String[] args) {
List<Integer> numbers =
Arrays.asList(3, 2, 5, 7, 4);
ObjLongConsumer<List<Integer>> operation =
(list, value) -> {
for (int number : list) {
System.out.println(number * value);
}
};
operation.accept(numbers, 10L);
}
}
Output
30 20 50 70 40
Explanation: The lambda iterates through the List<Integer> and multiplies each element by the supplied long value 10L.
Note: ObjLongConsumer<T> accepts an object and a primitive long, while BiConsumer<T, U> accepts two reference-type arguments.
ObjLongConsumer vs BiConsumer
Both ObjLongConsumer and BiConsumer represent operations that accept two arguments and return no result. The main difference is the type of their second argument.
| Feature | ObjLongConsumer<T> | BiConsumer<T, U> |
|---|---|---|
| First argument | Reference type T | Reference type T |
| Second argument | Primitive long | Reference type U |
| Return value | void | void |
| Abstract method | accept(T, long) | accept(T, U) |
| Primitive specialization | Yes | No |
When to Use ObjLongConsumer?
ObjLongConsumer is useful when an operation:
- Requires an object and a long value as inputs.
- Performs an action without producing a return value.
- Needs to work directly with a primitive long to avoid unnecessary boxing.
- Can be conveniently implemented using a lambda expression or method reference.