[英]How to change lambda expression to method reference
I need help regarding changing lambda expression to method reference: 我需要有关将lambda表达式更改为方法参考的帮助:
lambda expression: lambda表达式:
intervalCodes.stream().forEach(code -> {
modProfile.addIntervalUsageCode(createIntervalCode(code));
});
Can I change the above expression to like this: 我可以将上面的表达式更改为这样:
intervalCodes.stream().forEach(modProfile::addIntervalUsageCode(createIntervalCode));
Any suggestions please? 有什么建议吗?
Assuming createIntervalCode()
is a side-effect-free instance method, you can split your lambda into two method references: 假设
createIntervalCode()
是没有副作用的实例方法,则可以将lambda拆分为两个方法引用:
intervalCodes.stream()
.map(this::createIntervalCode)
.forEach(modProfile::addIntervalUsageCode);
If it's a static method, use ClassName::createIntervalCode
. 如果是静态方法,请使用
ClassName::createIntervalCode
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.