A method reference cannot capture variables. Thus, an exciting lambda cannot be directly converted to a method reference. For instance,
int x = 1; numbers.replaceAll(n -> n + x);
In some cases, if only one variable is fixed, it may be possible to convert the lambda to a method reference to the captured variable. For instance,
String greeting = "Hello, "; people.replaceAll(name -> greeting + name);
Can be converted to a method reference as
people.replaceAll(greeting::concat);
Misha source share