Since Java 8 comes with powerful lambda expressions,
I would like to write a function to convert a List/array of Strings to array/List of Integers, Floats, Doubles etc..
In normal Java, it would be as simple as
for(String str : strList){
intList.add(Integer.valueOf(str));
}
But how do I achieve the same with a lambda, given an array of Strings to be converted to an array of Integers.
You can also use,
In addition - control when string array doesn't have elements:
For List :
For Array :
Arrays.toString(int []) works for me.
EDIT: As pointed out in the comments, this is a much simpler version:
Arrays.stream(stringArray).mapToInt(Integer::parseInt).toArray()
This way we can skip the whole conversion to and from a list.I found another one line solution, but it's still pretty slow (takes about 100 times longer than a for cycle - tested on an array of 6000 0's)
What this does: