How can I convert List <List <Double>> to List <Double []> and enumerate <double []> using Java 8 Stream API?

I am stuck with this issue. I found similar answers here, but none of them solve the problem. Should I use mapToDouble () here? Is there something like "mapToDoubleArray"?

+3


source to share


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







All Articles