リストのリストに設定し、<整数>のコレクションを変換します

ZAK ZAK:

私が持っています HashMap<Integer, Set<Integer>>.

私は、変換したいCollectionリストのリストにマップ内のセットのを。

例えば:

import java.util.*;
import java.util.stream.*;
public class Example {

         public static void main( String[] args ) {
             Map<Integer,Set<Integer>> map = new HashMap<>();
             Set<Integer> set1 = Stream.of(1,2,3).collect(Collectors.toSet());
             Set<Integer> set2 = Stream.of(1,2).collect(Collectors.toSet());
             map.put(1,set1);
             map.put(2,set2);

             //I tried to get the collection as a first step
             Collection<Set<Integer>> collectionOfSets = map.values();

             // I neeed  List<List<Integer>> list = ...... 

             //so the list should contains[[1,2,3],[1,2]]
         }
    }
ユージン:
 map.values()
    .stream()
    .map(ArrayList::new)
    .collect(Collectors.toList());

あなたのスタートが良かった:のために行くmap.values()から始めること。あなたはそれをストリーミングする場合さて、ストリーム内の各要素があることを行っているCollection<Integer>(各個別の値)。あなたはAに、各値を変換したいですListこの場合、私が提供したArrayList受け入れコンストラクタを有しCollection、従ってArrayList::new方法の参照の使用を。そして最後に、これらすべての個々の値は、(に変換後List)新しいに収集されList経由Collectors.toList()

おすすめ

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