[英]How to convert a Collection<Collection<Double>> to a List<List<Double>>?
I have the following field 我有以下字段
Collection<Collection<Double>> items
and I want to convert it to a 我想把它转换成一个
List<List<Double>> itemsList
I know that I can convert a Collection
to a List
by list.addAll(collection)
, but how can I convert a Collection
of Collection
s? 我知道我可以通过list.addAll(collection)
将Collection
转换为List
,但是如何转换Collection
of Collection
?
You can use Streams : 您可以使用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>>
or with a method reference instead of the lambda expression : 或者使用方法引用而不是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>>
A Java 7 solution would require a loop : Java 7解决方案需要循环:
List<List<Double>> itemsList = new ArrayList<List<Double>>();
for (Collection<Double> col : items)
itemsList.add(new ArrayList<Double>(col));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.