Consumer Interface in Java - Quiz
Total: 5 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)
4. How is Consumer different from Supplier, Function and Runnable?
How is Consumer different from Supplier, Function and Runnable?
The only difference is the presence of an input and an output:
Consumer<T> void accept(T t) // T -> void
Supplier<T> T get() // void -> T
Function<T, R> R apply(T t) // T -> R
Runnable void run() // void -> void
Consumer takes an argument and returns nothing, Supplier is the opposite - no arguments, one returned value, Function takes an argument and returns a result, Runnable takes nothing and returns nothing. All four are stateless and describe exactly one action. A clear sign of the wrong choice: the body of a Consumer writes into an external list or field just to carry a result outside - that case calls for Function and map() instead of forEach().
5. Which relatives of Consumer live in the java.util.function package and why do they exist?
Which relatives of Consumer live in the java.util.function package and why do they exist?
One variant for two arguments and several for primitives - the primitive ones exist to avoid autoboxing:
BiConsumer<T, U> void accept(T t, U u) // (T, U) -> void, e.g. Map.forEach()
IntConsumer void accept(int value) // int -> void, IntStream.forEach()
LongConsumer void accept(long value) // long -> void
DoubleConsumer void accept(double value) // double -> void
ObjIntConsumer<T> void accept(T t, int value) // (T, int) -> void, accumulators in collect()
BiConsumer, IntConsumer, LongConsumer and DoubleConsumer also declare andThen(), taking a parameter of their own type. ObjIntConsumer and its siblings have no andThen() at all.
IntConsumer printSquare = n -> System.out.println(n * n);
IntStream.rangeClosed(1, 3).forEach(printSquare);
// 1
// 4
// 9