String to ArrayList<Long>

Viewed 2891

Currently i have:

String a = "123.5950,555,5973.1,6321.905,6411.810000000001,6591.855"

I can turn it into an array list of Strings then into array list of Longs:

ArrayList<String> vals = new ArrayList<String>(Arrays.asList(a.split(","));
ArrayList<Long> longs = new ArrayList<>();

for(String ks : vals){
       longs.add(Long.parseLong(ks));
}

I tried to do this with Stream to make this more 'fun' but cant seem to be successful with this:

ArrayList<Long> longs = a.stream().map(Long::parseLong).collect(Collectors.toList());

I dont think the for loop is very elegant, how can i do it with Stream?

Edit: copied to original string wrong

3 Answers
Related