February 2020
Intermediate to advanced
412 pages
9h 36m
English
The Consumer<T> interface has a single abstract method, accept(), that takes a T argument and performs an action with it but does not return any value. Using an anonymous class, we can create a Consumer<String> that simply prints the string as shown in the following code snippet:
import java.util.function.Consumer;public class A_08 { public static void main(String[] args) { Consumer<String> printConsumer = new Consumer<String>() { @Override public void accept(String s) { System.out.println(s); } }; printConsumer.accept("Hello World!"); }}
The output is as follows:
Hello World!
It can be implemented using a lambda expression as follows:
import java.util.function.Consumer;public class A_09 { public static void main ...
Read now
Unlock full access