Is there any built-in method in Java which allows us to convert comma separated String to some container (e.g array, List or Vector)? Or do I need to write custom code for t
Arrays.asList
returns a fixed-size List
backed by the array. If you want a normal mutable java.util.ArrayList
you need to do this:
List list = new ArrayList(Arrays.asList(string.split(" , ")));
Or, using Guava:
List list = Lists.newArrayList(Splitter.on(" , ").split(string));
Using a Splitter
gives you more flexibility in how you split the string and gives you the ability to, for example, skip empty strings in the results and trim results. It also has less weird behavior than String.split
as well as not requiring you to split by regex (that's just one option).