我是java的新手,所以我的问题是为什么这个工作:
这个方法在我的Tree类中:
public Stream flattened() {
return Stream.concat(
Stream.of(this),
children.stream().flatMap(Tree::flattened));
}
flatMap想要一个带有t作为param的函数,而flattened方法根本没有输入参数
这里发生了什么事?
解决方法:
函数调用中确实存在隐藏参数.因为flattened是一个非静态方法,所以函数中有一个隐含参数,这就是所谓的.
基本上,您在流中的每个对象上调用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
If the compile-time declaration is an instance method, then the target
reference is the first formal parameter of the invocation method.
Otherwise, there is no target reference
标签:java,java-8,java-stream
来源: https://codeday.me/bug/20190823/1701099.html