Java8ストリームは、変数を解決することはできません

チャールズ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()); 

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=185495&siteId=1