Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does '->' do in Java?

Tags:

java

java-8

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()));
like image 831
PuffySparrow Avatar asked Dec 25 '13 09:12

PuffySparrow


1 Answers

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; }
}
like image 155
assylias Avatar answered Oct 04 '22 03:10

assylias