How can I convert List <List <Double>> to List <Double []> and enumerate <double []> using Java 8 Stream API?
1 answer
To convert a List<List<Double>>
to List<double[]>
, you need to map each inner list to a double array using mapToDouble
in conjunction with toArray()
(which is the "mapToDoubleArray" operation you are looking for).
List<double[]> res =
myList.stream()
.map(l -> l.stream().mapToDouble(d -> d).toArray())
.collect(toList());
If you want instead List<double[]>
, you can just use .map(list -> list.toArray(new Double[list.size()]))
.
+5
source to share