[英]java stream - flatmap with method reference
我是java的新手,所以我的问题是为什么这个工作:
这个方法在我的Tree
类中:
public Stream<Tree> flattened() {
return Stream.concat(
Stream.of(this),
children.stream().flatMap(Tree::flattened));
}
flatMap想要一个带有t作为param的函数,而flattened方法根本没有输入参数
这里发生了什么事?
函数调用中确实存在隐藏参数。 因为flattened
是一个非静态方法中,存在在你的函数,其被称为隐式参数this
。
基本上,您在流中的每个对象上调用flattened
,其中所述元素是您的参数。
编辑(澄清): Tree::flattened
可能意味着两件事之一。 这可能意味着:
tree -> Tree.flattened(tree) //flattened is a static method, which yours is not
或者它也可能意味着:
tree -> tree.flattened() //flattened is an instance method, as in your case
除此之外,它还可能意味着:
tree -> this.flattened(tree) //also doesn't apply to your case
来自JLS :
如果编译时声明是实例方法,则目标引用是调用方法的第一个形式参数。 否则,没有目标参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.