Is there any tested library method to convert String "1, 2, 3, 4" or "1 2 3 4" to array of integers or List?
I can write it myself, just library is quick and very readable.
Is there any tested library method to convert String "1, 2, 3, 4" or "1 2 3 4" to array of integers or List?
I can write it myself, just library is quick and very readable.
There is no built-in Java API method for this, but writing a helper method yourself is easy enough:
ArrayList<Integer> result = new ArrayList<Integer>();
for (String token : "1 2, 3 4".split("[ ,]+"))
result.add(Integer.parseInt(token));
// result now holds { 1, 2, 3, 4 }
In Java 8 you could also take advantage of the new Streams concept and its map()
method to shorten the code further.