Lambda引用字段 [英] Lambda reference to a field
本文介绍了Lambda引用字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何获得lambda对字段的引用。
我不想使用方法,因为我的字段是公共最终的。
我怀疑这是不可能的,但我没有看到明显的陈述。
I'd like to know how to get lambda reference to a field. I don't want to use a method because my field is public final. I suspect this is impossible but I don't see an obvious statement.
class A {
public final String id;
...
}
Map<String, A> f(List<A> l) {
return l.stream().collect(Collectors.toMap(A::id, Function.identity()));
}
推荐答案
你总是可以使用lambda表达式:
You can always use a lambda expression:
return l.stream().collect(Collectors.toMap(a -> a.id, Function.identity()));
我认为方法引用由于某种原因以这种方式调用,因此仅适用于方法。
I think that "method references" are called this way for a reason, and therefore apply only for methods.
这篇关于Lambda引用字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文