15

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?

2
  • Why not double[]? It's faster. Commented Feb 1, 2012 at 18:40
  • Loop and convert each one of them, is what you can do. Commented Feb 1, 2012 at 18:40

8 Answers 8

39

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);
Sign up to request clarification or add additional context in comments.

Comments

13

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.

Comments

6

In one line :p

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

Comments

4

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

1 Comment

this should work with map as well as mapToDouble (and might be negligibly faster?)
2

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.

Comments

2

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

Comments

1
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

2 Comments

You were missing a semicolon and the formatting made it difficult to read; fixed that. It's also worth noting that this is not part of core java but rather part of org.apache.commons.collections - that being said, excellent answer. (I'd +1 you but I'm out of votes for the day)
lol! thanks for the edit! I kind of like commons collections for this purposes!
0

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.

2 Comments

Except that you are missing a type in your for loop; and if he wants to get an array out (as opposed to a list that he can append to), you will actually need to know the integer index.
I didn't clarify that the code I provided is a snip from a Beanshell script that I worked on, hence the lack of a data type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.