多行lambda的编译问题

多行lambda的编译问题,lambda,java-8,Lambda,Java 8,我在看 清单6似乎没有为我编译。我尝试使用ArrayList和对象的ArrayList int count = anArrayList.stream() .map(e -> { System.out.println("Saw " + e); e }) .count(); 我得到的错误是: The method map(java.util.function.Function<? super java.lang.In

我在看

清单6似乎没有为我编译。我尝试使用
ArrayList
和对象的ArrayList

int count = 
    anArrayList.stream()
               .map(e -> { System.out.println("Saw " + e); e })
               .count();
我得到的错误是:

The method map(java.util.function.Function<? super java.lang.Integer,? extends R extends java.lang.Object>) in the type Stream<Integer> is not applicable for the arguments (java.util.function.Function<? super java.lang.Integer,? extends R extends java.lang.Object>)
方法映射(java.util.function.functionChange

.map(e -> { System.out.println("Saw " + e); e })

它应该会起作用

花括号的内容是一个函数,语法上必须“返回”它的返回值。分号不是可选的

这似乎是网站上的打字错误。
还要注意,在“真实”代码中,您不希望在流处理过程中包含像System.out.println这样的副作用。

当回答“不适用”时,您使用哪一个(版本)编译器??我尝试过的所有编译器都通过报告语法错误来正确回答。是的,这是一个语法错误。“…不适用…”是IDE(eclipse)显示的提示。可能应该提到这一点。:)经验教训:在考虑语义分析(如类型检查)之前,一定要先注意语法错误:)
.map(e -> { System.out.println("Saw " + e); return e; })