The DoubleConsumer interface is a specialized functional interface available in the java.util.function package, introduced in Java 8. It represents an operation that accepts a single primitive double value as input and performs an action without returning any result.
- Primitive specialization of Consumer<Double>.
- Avoids boxing and unboxing overhead.
- Supports lambda expressions and method references.
import java.util.function.DoubleConsumer;
public class Main {
public static void main(String[] args) {
DoubleConsumer area =
radius -> System.out.println(
3.14 * radius * radius);
area.accept(2.0);
}
}
Output
12.56
Explanation: In this example, a DoubleConsumer accepts a radius value and calculates the area of a circle. The result is printed directly without returning any value.
Syntax
@FunctionalInterface
public interface DoubleConsumer {
void accept(double value);
default DoubleConsumer andThen(DoubleConsumer after) {
// implementation
}
}
Methods of DoubleConsumer Interface
1. accept() Method
The accept() method is the functional method of the DoubleConsumer interface. It accepts a single double value and performs the specified operation without returning any result.
- Performs side-effect operations.
- Avoids boxing and unboxing overhead.
Syntax
void accept(double value)
import java.util.function.DoubleConsumer;
public class Main {
public static void main(String[] args) {
DoubleConsumer display =
num -> System.out.println(num * 10);
display.accept(3);
}
}
Output
30.0
Explanation: In this example, a DoubleConsumer accepts the value 3, multiplies it by 10, and prints the result. Since DoubleConsumer does not return a value, the operation is performed directly.
2. andThen() Method
The andThen() method returns a composed DoubleConsumer that performs the current operation first and then executes another specified DoubleConsumer.
- Returns a composed DoubleConsumer.
- Supports functional programming style.
Syntax:
default DoubleConsumer andThen(DoubleConsumer after)
Example 1: Chaining Operations with andThen()
import java.util.function.DoubleConsumer;
public class Main {
public static void main(String[] args) {
DoubleConsumer square =
num -> System.out.println("Square = " + (num * num));
DoubleConsumer cube =
num -> System.out.println("Cube = " + (num * num * num));
DoubleConsumer result =
square.andThen(cube);
result.accept(3);
}
}
Output
Square = 9.0 Cube = 27.0
Explanation: The andThen() method combines two operations. First, it prints the square of 3, and then it prints the cube of 3.
Example 2: NullPointerException with andThen()
import java.util.function.DoubleConsumer;
public class Main {
public static void main(String[] args) {
try {
DoubleConsumer display =
num -> System.out.println(num);
DoubleConsumer result =
display.andThen(null);
result.accept(3);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output
Exception: java.lang.NullPointerException
Explanation: In this example, null is passed to the andThen() method instead of a valid DoubleConsumer. Therefore, Java throws a NullPointerException.
Example 3: Exception Propagation in andThen()
import java.util.function.DoubleConsumer;
public class Main {
public static void main(String[] args) {
try {
DoubleConsumer convert =
num -> System.out.println(
Integer.parseInt(Double.toString(num)));
DoubleConsumer display =
num -> System.out.println(num);
DoubleConsumer result =
display.andThen(convert);
result.accept(3.5);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output
3.5 Exception: java.lang.NumberFormatException: For input string: "3.5"
Explanation: The first operation prints the value successfully. The second operation attempts to convert "3.5" into an integer using Integer.parseInt(), which causes a NumberFormatException.
DoubleConsumer vs Consumer<Double>
| Feature | DoubleConsumer | Consumer |
|---|---|---|
| Input Type | double | Double |
| Boxing/Unboxing | Not Required | Required |
| Performance | Faster | Slightly Slower |
| Return Value | None | None |
| Package | java.util.function | java.util.function |
Advantages of DoubleConsumer
- Avoids boxing and unboxing overhead.
- Improves performance when working with primitive double values.
- Simplifies code using lambda expressions.
- Supports functional programming in Java.
- Enables operation chaining using andThen().
- Useful for calculations, logging, and data processing.