
Part-3 Functional Interface (Consumer)
Consumer: Consumer is an interface in java. A functional interface in java.util.function . Represents an operation that takes one input and returns nothing. Core method: void accept(T t) Extra Methods in Consumer: andThen(Consumer after) Chains two Consumers together. First executes the current Consumer, then executes the after Consumer. Consumer<String> print = s -> System.out.println(s); Consumer<String> printUpper = s -> System.out.println(s.toUpperCase()); Consumer<String> combined = print.andThen(printUpper); combined.accept("hello"); // Output: // hello // HELLO Specialized Consumers: Java also provides specialized versions of Consumer for different use cases: BiConsumer<T,U> -Accepts two inputs, performs an action. BiConsumer<String, Integer> printNameAge = (name, age) -> System.out.println(name + " is " + age + " years old"); printNameAge.accept("Ravi", 25); // Output: Ravi is 25 years old Primitive Consumer Interfaces: Normal Consumer uses objects (like Integer), but when deal
Continue reading on Dev.to
Opens in a new tab


