Is there a way to cast an Object[] array into double[] array without using any loops. And cast Double[] array to double[] array
-
This answers the second part of your question: stackoverflow.com/questions/1109988/…Barney– Barney2013-02-23 19:09:12 +00:00Commented Feb 23, 2013 at 19:09
-
The above link says, I have to use loop, which I want to avoid. Java definitely failed here in comparison to .net (stackoverflow.com/questions/3741350/…)Osman Khalid– Osman Khalid2013-02-26 05:55:59 +00:00Commented Feb 26, 2013 at 5:55
Add a comment
|
1 Answer
In 2013 we haven't Java Stream API, just in march of 2014. With it you can get your answer:
From Object[] to double[]
Object[] objectArray = {1.0, 2.0, 3.0};
double[] convertedArray = Arrays.stream(objectArray) // converts to a stream
.mapToDouble(num -> Double.parseDouble(num.toString())) // change each value to Double
.toArray(); // converts back to array
From Double[] to double[]
Double[] doubleArray = {1.0, 2.0, 3.0};
double[] conv = Arrays.stream(doubArray)
.mapToDouble(num -> Double.parseDouble(num.toString()))
.toArray();
You notice that it's the same operation, since the resulting type for both conversions are double[]. What is changed is the source data.
PS: what a late answer :|
1 Comment
Osman Khalid
Better late than never :)