Kathir Kathir - 5 months ago 21
Java Question

Convert Comma Separated Values to List<Long>

Assume I have a set of numbers like

1,2,3,4,5,6,7
input as a single
String
. I would like to convert those numbers to a
List
of
Long
objects ie
List<Long>
.

Can anyone recommend the easiest method?

Answer

I would use the excellent google's Guava library to do it. String.split can cause many troubles.

String numbers="1,2,3,4,5,6,7";
Iterable<String> splitIterator = Splitter.on(',').split(numbers);
List<String> list= Lists.newArrayList(splitIterator );