Given the code fragment:
List<String> nL = Arrays.asList("Jim", "John", "Jeff");
Function<String, String> funVal = s -> "Hello : ".concat(s);
nL.stream()
.map(funVal)
.peek(System.out::print);
What is the result?
A.
Hello : JimHello : JohnHello : Jeff
B.
JimJohnJeff
C. The program prints nothing.
D. A compilation error occurs.
题解
这个题目提供的代码并不会输出任何东西,若将第 22 行开始的程序修改如下:
List<String> nL = Arrays.asList("Jim", "John", "Jeff");
Function<String, String> funVal = s -> "Hello : ".concat(s);
nL.stream()
.map(funVal)
.forEach(System.out::print);
或:
nL.stream()
.map(funVal)
.peek(System.out::print)
.count();
才会输出:
Hello : JimHello : JohnHello : Jeff
