java - Java8 流无法解析变量
问题描述
我是 Java8 的新手,我想重构这段代码并将其转换为更 Java8 的风格,
for (RestaurantAddressee RestaurantAddressee : consultationRestaurant.getAddressees()) {
Chain chain = chainRestService.getClient().getChainDetails(getTDKUser(), RestaurantAddressee.getChain().getId());
if (chain.getOrganisation().getId().equalsIgnoreCase(event.getOrganisationId())) {
chainIds.add(restaurantAddressee.getChain().getId());
}
}
所以我为此代码更改它:
consultationRestaurant.getAddressees()
.stream()
.map( ma -> chainRestService.getClient().getChainDetails(getTDKUser(), ma.getChain().getId()))
.filter(chain -> chain.getOrganisation().getId().equalsIgnoreCase(event.getOrganisationId()))
.forEach(chainIds.add(chain.getId()));
但我有这个编译错误:
链无法解决
解决方案
您忘记在forEach
调用中指定 lambda 表达式参数。
也就是说,您不应该使用forEach
将元素添加到集合中。使用collect
:
List<String> chainIds =
consultationRestaurant.getAddressees()
.stream()
.map( ma -> chainRestService.getClient().getChainDetails(getTDKUser(), ma.getChain().getId()))
.filter(chain -> chain.getOrganisation().getId().equalsIgnoreCase(event.getOrganisationId()))
.map(Chain::getId)
.collect(Collectors.toList());
推荐阅读
- bixby - Bixby 在胶囊中的特定点触发 OAuth 流
- c++ - Converting String for Delphi to std::string for C++
- xcode - 构建停止而没有先前的错误
- r - plm() 出错;替换有 2473 行,数据有 2475
- java - 在java中将unicode值转换为字符串
- gcc - List of pairs of strings initialization in constructor
- python - 如何自动化不是字符串的函数返回值?
- jmespath - 如何在多个对象中插入具有动态值的属性或在多个数组中动态添加元素?
- unity3d - 在 Unity 中访问 TM Pro InputField 文本
- php - Laravel + Vue 上的随机 javascript 运行时错误