问题
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