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()