I want to replace lambda expression by method reference in the below example :
public class Example {
public static void main(String[] args) {
List<String> words = Arrays.asList("toto.", "titi.", "other");
//lambda expression in the filter (predicate)
words.stream().filter(s -> s.endsWith(".")).forEach(System.out::println);
}
}
I want to write a something like this :
words.stream().filter(s::endsWith(".")).forEach(System.out::println);
is it possible to transform any lambda expression to method reference.
::if the argument is on the instance call. You can replaces -> "hi".equals(s)with"hi"::equalsbut not if it'ss -> s.equals("hi")MethodHandlecan shuffle arguments, but the result is not a direct method handle anymore andLambdaMetaFactorydoes support direct method handles only. Partially applied functions, on the other hand, would work, as they don’t shuffle arguments, and LMF supports left-to-right parameter binding. So for.endsWith("."), where the right parameter ought to be bound, no chance…String::endsWith, plus some hypothetical way to bind the"."argument. What’s the advantage overs -> s.endsWith(".")?