Functional Interface Consumer - Quiz
Total: 3 questions
1. When java.util.function.Consumer interface can be used?
When java.util.function.Consumer interface can be used?
Consumer is used when an object should be taken as input and some operation should be performed on the object without returning any result. Common example of using Consumer interface is printing.
2. Write lambda expression, which uses java.util.function.Consumer interface, for printing details of Exam object.
public class Exam {
private String name;
private String version;
public Exam(String name, String version) {
this.name = name;
this.version = version;
}
public String getName() {
return name;
}
public String getVersion() {
return version;
}
}
Write lambda expression, which uses java.util.function.Consumer interface, for printing details of Exam object.
public class Exam {
private String name;
private String version;
public Exam(String name, String version) {
this.name = name;
this.version = version;
}
public String getName() {
return name;
}
public String getVersion() {
return version;
}
}Consumer<Exam> consumer = e -> System.out.println(e.getName() +
" " + e.getVersion());
consumer.accept(new Exam("OCPJP", "8"));
3. The default method of the Consumer interface.
The default method of the Consumer interface.
default Consumer<T> andThen(Consumer<? super T> after)
Page 1 of 1