java方法引用放到map_java stream – 带方法引用的flatmap

我是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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值