gpt4 book ai didi

Java8 .getMethod() 与::getMethod

转载 作者:搜寻专家 更新时间:2023-11-01 02:35:48 27 4
gpt4 key购买 nike

我是 Java8 的新手,我创建了这段运行良好的代码

 userService.getClient().findUsersByMarkets(marketIds)
.stream()
.filter(us -> !alreadyNotifiedUserIds.contains(us.getId()))
.forEach(usersToBeNotified::add);

但根据我的理解,这段代码也应该可以正常工作,但事实并非如此,我想知道为什么

     userService.getClient().findUsersByMarkets(marketIds)
.stream()
.filter(us -> !alreadyNotifiedUserIds.contains(User::getId))
.forEach(usersToBeNotified::add);

最佳答案

User::getId 是对函数的引用,因此它不等同于 contains(us.getId())

参见 --> https://www.codementor.io/eh3rrera/using-java-8-method-reference-du10866vx熟悉方法引用。

关于Java8 .getMethod() 与::getMethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53560419/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com