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);

Create a method implementing it using a loop, then call your method, and you'll have a one-line solution.

There is no buit-in method in the Java API to do that.

Tags:

Java