Functional Interface Function - Quiz
Total: 5 questions
1. The main purpose java.util.function.Function interface.
The main purpose java.util.function.Function interface.
The main purpose of this interface is mapping scenarios - when an object of one type is taken as input and it is converted (or mapped) to another type.
2. Write lambda expression, which uses java.util.function.Function interface to convert Exam object to String 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.Function interface to convert Exam object to String 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;
}
}Function<Exam, String> converter = e -> e.getName() + " " + e.getVersion();
System.out.println(converter.apply(new Exam("OCPJP", "8")));
3. The default methods of Function interface.
The default methods of Function interface.
default <V> Function<V,R> compose(
Function<? super V,? extends T< before)
default <V> Function<T,V> andThen(
Function<? super R,? extends V> after)
4. The difference between the compose and andThen method of the Functional interface.
The difference between the compose and andThen method of the Functional interface.
The compose method applies the function represented by the parameter first, and its result serves as the input to the other function. The andThen method first applies the function that calls the method, and its result acts as the input of the function represented by the parameter.
5. A static method of the Function interface.
A static method of the Function interface.
static <T> Function<T, T> identity()