[英]How to convert a Collection<Collection<Double>> to a List<List<Double>>?
我有以下字段
Collection<Collection<Double>> items
我想把它轉換成一個
List<List<Double>> itemsList
我知道我可以通過list.addAll(collection)
將Collection
轉換為List
,但是如何轉換Collection
of Collection
?
您可以使用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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.