is there a lambda function that cannot be a method reference

笑着哭i 提交于 2019-12-05 09:51:51

Method reference cannot capture variables. So a capturing lambda cannot be directly converted to a method reference. For example,

int x = 1;
numbers.replaceAll(n -> n + x);

In some cases, if only one variable is captured, it might be possible to convert lambda to a method reference on the captured variable. For example,

String greeting = "Hello, ";

people.replaceAll(name -> greeting + name);

Can be converted to method reference as

people.replaceAll(greeting::concat);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!