任何人都可以解释为什么下面的代码不能编译但第二个代码呢?
不要编译
private void doNotCompile() {
List<Integer> out;
out = IntStream
.range(1, 10)
.filter(e -> e % 2 == 0)
.map(e -> Integer.valueOf(2 * e))
.collect(Collectors.toList());
System.out.println(out);
}
Run Code Online (Sandbox Code Playgroud)
收集行上的编译错误
编译
private void compiles() {
List<Integer> in;
in = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
List<Integer> out;
out = in.stream()
.filter(e -> e % 2 == 0)
.map(e -> 2 * e)
.collect(Collectors.toList());
System.out.println(out);
}
Run Code Online (Sandbox Code Playgroud)