チャールズXuriguera:
、私は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
コール。
それはあなたが使用してはならない、と述べた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());