The ObjDoubleConsumer interface is a functional interface from the java.util.function package, introduced in Java 8. It represents an operation that accepts an object of type T and a primitive double value and does not return a result.
- The first argument is an object of generic type T, while the second argument is a primitive double.
- Its single abstract method is accept(T t, double value), which can be implemented using a lambda expression or method reference.
Syntax of ObjDoubleConsumer
The ObjDoubleConsumer interface can be declared as:
ObjDoubleConsumer<T> consumer;
Here, T represents the type of the object passed as the first argument. The second argument is always a primitive double.
Method of ObjDoubleConsumer
The ObjDoubleConsumer interface provides one abstract method, accept(), which performs an operation using the supplied object and double value.
accept()
The accept() method accepts an object of type T and a primitive double value. It performs the specified operation using both arguments without returning a result.
Syntax:
void accept(T t, double value)
Parameters:
- t: The object of type T passed as the first argument.
- value: The primitive double value passed as the second argument.
Return Type: The method does not return any value (void).
Example: ObjDoubleConsumer to multiply every element of a list by a given double value.
import java.util.Arrays;
import java.util.List;
import java.util.function.ObjDoubleConsumer;
public class GFG {
public static void main(String[] args) {
List<Integer> numbers =
Arrays.asList(3, 2, 5, 7, 4);
ObjDoubleConsumer<List<Integer>> operation =
(list, value) -> {
for (int number : list) {
System.out.println(number * value);
}
};
operation.accept(numbers, 2.0);
}
}
Output
6.0 4.0 10.0 14.0 8.0
Explanation: The lambda expression implements the accept() method. It receives the List<Integer> as the first argument and 2.0 as the second argument. It iterates through the list, multiplies each element by 2.0, and prints the result. Since ObjDoubleConsumer represents a consumer operation, it does not return a value.
ObjDoubleConsumer vs BiConsumer
Both ObjDoubleConsumer and BiConsumer accept two arguments and return no result. The main difference is the type of their second argument.
| Feature | ObjDoubleConsumer<T> | BiConsumer<T, U> |
|---|---|---|
| First argument | Reference type T | Reference type T |
| Second argument | Primitive double | Reference type U |
| Return type | void | void |
| Functional method | accept(T, double) | accept(T, U) |
| Primitive specialization | Yes | No |
Note: ObjDoubleConsumer<T> avoids boxing a primitive double into a Double object, which would be required when using BiConsumer<T, Double>.
When to Use ObjDoubleConsumer?
ObjDoubleConsumer is useful when an operation requires an object and a primitive double as inputs but does not need to return a result. It is particularly suitable for operations involving primitive double values because it avoids unnecessary boxing and unboxing.