How to convert String Array to Double Array in one line

Java 8 Stream API allows to do this:

double[] doubleValues = Arrays.stream(guaranteedOutput)
                        .mapToDouble(Double::parseDouble)
                        .toArray();

Double colon is used as a method reference. Read more here.

Before using the code don’t forget to import java.util.Arrays;

UPD: If you want to cast your array to Double[], not double[], you can use the following code:

Double[] doubleValues = Arrays.stream(guaranteedOutput)
                        .map(Double::valueOf)
                        .toArray(Double[]::new);

Leave a Comment