daydreamer
daydreamer

Reputation: 92169

How to convert String Array to Double Array in one line

I have a string array as:

String[] guaranteedOutput = Arrays.copyOf(values, values.length,
String[].class);

All the String values are numbers. The data should be converted to a Double[].

Question
Is there a one line solution in Java to achieve this or we need to loop and convert each value to a Double?

Upvotes: 15

Views: 83775

Answers (8)

Michael Berdyshev
Michael Berdyshev

Reputation: 1513

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

Upvotes: 39

Yeabkal Wubshit
Yeabkal Wubshit

Reputation: 41

This will convert an array of strings to an array of doubles in one line.

String[] stringArray = {"1.3", "4.6", "3.2"};
double[] doubleArray = Arrays.stream(stringArray).mapToDouble(Double::parseDouble).toArray();

Upvotes: 4

alain.janinm
alain.janinm

Reputation: 20065

In one line :p

Double[] d=new ArrayList<Double>() {{for (String tempLongString : tempLongStrings) add(new Double(tempLongString));}}.toArray(new Double[tempLongStrings.length]);

Upvotes: 6

Arthur Neves
Arthur Neves

Reputation: 12148

CollectionUtils.collect(guaranteedOutput, new Transformer() { 
        public Object transform(Object i) { 
            return Double.parseDouble(i); 
        }  
});

EDIT

Keep in mind that this is no in JavaSDK ! I am using http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html

Upvotes: 1

Adam Mihalcin
Adam Mihalcin

Reputation: 14478

You want a map operation from functional programming, which unfortunately Java does not offer. Instead, you have to loop as

double[] nums = new double[guaranteedOutput.length];
for (int i = 0; i < nums.length; i++) {
    nums[i] = Double.parseDouble(guaranteedOutput[i]);
}

Upvotes: 2

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77505

What is wrong with a loop?

double[] parsed = new double[values.length];
for (int i = 0; i<values.length; i++) parsed[i] = Double.valueOf(values[i]);

is not particularly clumsy. Plus, you can easily add proper error handling.

Of course you can easily wrap this as you like.

OpenJDK8 will probably bring lambda expressions, and using Double.valueOf as "map" function would be a prime example for using this.

Upvotes: 2

Carlos
Carlos

Reputation: 1935

I think you're going to have to loop through and convert each value over to a double. This is how I did it in one of my other questions:

for(getData : getDataArray){
 double getTotal;
 getTotal = getTotal + Double.parseDouble(getData);
}
return getTotal;

The idea is the same. Turn that into a method, pass your paramters and you're golden.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692231

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.

Upvotes: 13

Related Questions