Java8流无法解析变量
发布时间:2020-12-15 08:28:08 所属栏目:Java 来源:网络整理
导读:我是 Java8的新手,我想重构这段代码并将其转换为更多的Java8风格, for (RestaurantAddressee RestaurantAddressee : consultationRestaurant.getAddressees()) { Chain chain = chainRestService.getClient().getChainDetails(getTDKUser(),RestaurantAddres
我是
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向集合中添加元素.使用收集: 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()); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |