I have a String containing double values such as:
String str = "0.1,0.4,0.9,0.1";
I want to get a double array from this string like this:
double[] arr = {0.1, 0.4, 0.9, 0.1};
The most obvious way to do this for me is:
String[] tokens = str.split(",");
double[] arr = new double[tokens.length];
int i=0
for (String st : tokens) {
arr[i++] = Double.valueOf(st);
}
is there some faster/better way to do that other than the one mentioned above in Java 8?
You can use Stream
s:
double[] arr = Stream.of(str.split(","))
.mapToDouble (Double::parseDouble)
.toArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With