我有以下领域
Collection<Collection<Double>> items
我想将其转换为
List<List<Double>> itemsList
我知道,我可以转换Collection
到List
通过list.addAll(collection)
,但我怎么能转换Collection
的Collection
S'
您可以使用Streams:
List<List<Double>> itemsList =
items.stream() // create a Stream<Collection<Double>>
.map(c->new ArrayList<Double>(c)) // map each Collection<Double> to List<Double>
.collect(Collectors.toList()); // collect to a List<List<Double>>
或使用方法引用代替lambda表达式:
List<List<Double>> itemsList =
items.stream() // create a Stream<Collection<Double>>
.map(ArrayList::new) // map each Collection<Double> to List<Double>
.collect(Collectors.toList()); // collect to a List<List<Double>>
Java 7解决方案将需要循环:
List<List<Double>> itemsList = new ArrayList<List<Double>>();
for (Collection<Double> col : items)
itemsList.add(new ArrayList<Double>(col));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句