Reputation: 389
I am new to lambda expressions. I have the below code:
List<String> someNumbers = Arrays.asList("N40", "N36", "B12", "B6", "G53", "G49", "G60", "G50", "G53", "I26", "I17", "I29", "O71");
someNumbers
.stream()
.filter(startsWith("G"))
In the above code, the 'filter' should act as a predicate and return a boolean value. But why does it show a compile error? I don't get an error when I use the below line:
.filter(s->s.startsWith("G"))
Above, the stream get passed to the filter. so what is the need for the argument s? for instance, '.map' processes it without any errors if used as
.map(String::toUppercase).
Upvotes: 1
Views: 128
Reputation: 120848
basic knowledge on method references I guess.
String::toUppercase
is equivalent to:
s -> s.toUppercase()
While:
startsWith("G")
would theoretically be equivalent to:
s -> s.startsWith("G")
This is simply not allowed by the language.
Upvotes: 3