我在看一些Java教程时,不确定“->”的作用,并且在谷歌上找不到任何有关的内容。
这里有一些使用它的代码:
myShapesCollection.stream()
.filter(e -> e.getColor() == Color.RED)
.forEach(e -> System.out.println(e.getName()));
lambda 表达式 的语法在 Java 8 中被使用。
例如,filter 需要一个 Predicate,并且 e -> e.getColor() == Color.RED 等同于:
new Predicate<Shape>() {
public boolean test(Shape s) { return s.getColor() == Color.RED; }
}
new Predicate<Shape>(){ public boolean test(Shape e){ return e.getColor() == Color.RED; } }
。 - Eng.Fouad