IntConsumer, LongConsumer, and DoubleConsumer
What you'll learn: How to use primitive specializations of Consumer to perform side effects on primitive values without boxing overhead.
Why Primitive Consumer Specializations Matter
You've already learned about Consumer<T> for performing side effects on objects. When working with primitive types (int, long, double), using Consumer<Integer> would require boxing each primitive value into a wrapper object. The functional interfaces IntConsumer, LongConsumer, and DoubleConsumer eliminate this overhead by accepting primitives directly.
The Three Primitive Consumers
Each interface has a single abstract method accept:
IntConsumer:void accept(int value)LongConsumer:void accept(long value)DoubleConsumer:void accept(double value)
Think of these as specialized workers: instead of handling "boxed packages" (wrapper objects), they work directly with raw materials (primitives), making the process faster and more efficient.
When You'll Use Them
These interfaces appear frequently in Stream API operations on primitive streams:
IntStream.range(1, 5)
.forEach(System.out::println); // forEach expects IntConsumer
You can create them explicitly with lambdas:
IntConsumer logger = value -> System.out.println("Value: " + value);
logger.accept(42); // No boxing occurs
Chaining with andThen
Like Consumer, these interfaces support composition:
IntConsumer print = System.out::println;
IntConsumer logSquare = x -> System.out.println("Square: " + (x * x));
IntConsumer combined = print.andThen(logSquare);
combined.accept(5); // Prints both value and square
Key Takeaway: IntConsumer, LongConsumer, and DoubleConsumer provide boxing-free side effects for primitive values, improving performance in primitive stream operations and reducing memory overhead compared to generic Consumer<T>.