What does '->' do in Java?

让人想犯罪 __ 提交于 2019-12-23 09:29:31

问题


I was looking at some java tutorials and wasn't sure what '->' did and couldn't find anything on google about it.

Here's some code that I saw that used it:

myShapesCollection.stream()
.filter(e -> e.getColor() == Color.RED)
.forEach(e -> System.out.println(e.getName()));

回答1:


That is the syntax used for lambda expressions, available in Java 8.

For example, filter expects a Predicate and e -> e.getColor() == Color.RED is functionally equivalent to:

new Predicate<Shape>() {
    public boolean test(Shape s) { return s.getColor() == Color.RED; }
}


来源:https://stackoverflow.com/questions/20771449/what-does-do-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!