beginnerFunctional Programming
What is the Consumer functional interface in Java, and when do you use it?
Consumer<T> takes one argument and returns nothing, via its abstract method void accept(T t) — it's for operations performed purely for their side effects, like printing or saving. It's the interface Stream.forEach() and List.forEach() expect, and its andThen() default method lets you chain multiple consumers to run in sequence on the same input.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response