如何将Collection <Collection <Double >>转换为List <List <Double >>?

用户7126137:

我有以下领域

Collection<Collection<Double>> items

我想将其转换为

List<List<Double>> itemsList

我知道,我可以转换CollectionList通过list.addAll(collection),但我怎么能转换CollectionCollectionS'

他们是 :

您可以使用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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将Scala List [List [Double]]转换为Java double [] []

将List <double>转换为double [n,1]

如何在Java中将List <Double>转换为double []?

如何使用Java 8流API将List <List <Double >>转换为List <Double []>和List <double []>?

如何将Double []转换为double []?

如何将double []转换为Double []?

Scala - 将 List[((String, String), Double)] 转换为 List[String,Map[String,Double]]

将List <double>转换为LiveCharts.IChartValues

将int转换为List <double?>,ChartJS Core

Scala Spark-将RDD [List [scala.Double]]转换为RDD [scala.Double]

将double [] []转换为Double [] []

无法从 Collection<> 转换为 List<>

如何将“System.Collection.Generic.List<String>”转换为“System.Windows.Documents.List”?

将double,double转换为double []

在Java中将scala Seq [Double]转换为Java List <Double>

如何创建将List <List <Integer >>和List <List <Double >>(可能更多)都转换为字符串的函数?

将Collection <Object>转换为List <String>

如何将“double”转换为“VARIANT”

将 List<List<double> 提取到 list<double>

如何将UnsafeMutablePointer <Double>数组转换为Double数组?

如何将 CollectionAccumulator[(Double, Double)] 转换为 SparkdataFrame?

在Scala中将Future [List [Error \ / Double]]转换为Future [[List [Error] \ / List [Double]]

使用double []的RAM的List <Double>?

Spray-json将部分json转换为Map [String,List [Double]]

为什么将List <double>显式转换为IEnumerable <object>会引发异常?

使用play框架将Json值转换为List [(T,Double)]

Tidyr:pivot_wider错误:无法将<double>转换为<list>

将C ++ double *转换为Java double

将double数组转换为Double ArrayList