Which statement is true about the single abstract method of the java.util.function.Function interface?



A. It accepts one argument and returns void.
B. It accepts one argument and returns boolean.
C. It accepts one argument and always produces a result of the same type as the argument.
D. It accepts an argument and produces a result of any data type.

題解

Function的JDK原始碼片段如下:

public interface Function<T, R> {
    R apply(T t);
}

選項A,要回傳R型態的物件才對。

選項B,要回傳R型態的物件才對。

選項C,傳入的參數型態不一定要跟回傳型態一樣。

選項D,正確。