How to convert comma-separated String to List?
How to convert comma-separated String to List?
Question
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 that?
String commaSeparated = "item1 , item2 , item3";
List<String> items = //method that converts above string into list??
Accepted Answer
Convert comma separated String to List
List<String> items = Arrays.asList(str.split("\\s*,\\s*"));
The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or more whitespace
which will place the words into the list and collapse any whitespace between the words and commas.
Please note that this returns simply a wrapper on an array: you CANNOT for example .remove()
from the resulting List
. For an actual ArrayList
you must further use new ArrayList<String>
.
Read more… Read less…
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<String> list = new ArrayList<String>(Arrays.asList(string.split(" , ")));
Or, using Guava:
List<String> 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).
Two steps:
String [] items = commaSeparated.split("\\s*,\\s*");
List<String> container = Arrays.asList(items);
Here is another one for converting CSV to ArrayList:
String str="string,with,comma";
ArrayList aList= new ArrayList(Arrays.asList(str.split(",")));
for(int i=0;i<aList.size();i++)
{
System.out.println(" -->"+aList.get(i));
}
Prints you
-->string
-->with
-->comma
If a List
is the end-goal as the OP stated, then already accepted answer is still the shortest and the best. However I want to provide alternatives using Java 8 Streams, that will give you more benefit if it is part of a pipeline for further processing.
By wrapping the result of the .split function (a native array) into a stream and then converting to a list.
List<String> list =
Stream.of("a,b,c".split(","))
.collect(Collectors.toList());
If it is important that the result is stored as an ArrayList
as per the title from the OP, you can use a different Collector
method:
ArrayList<String> list =
Stream.of("a,b,c".split(","))
.collect(Collectors.toCollection(ArrayList<String>::new));
Or by using the RegEx parsing api:
ArrayList<String> list =
Pattern.compile(",")
.splitAsStream("a,b,c")
.collect(Collectors.toCollection(ArrayList<String>::new));
Note that you could still consider to leave the list
variable typed as List<String>
instead of ArrayList<String>
. The generic interface for List
still looks plenty of similar enough to the ArrayList
implementation.
By themselves, these code examples do not seem to add a lot (except more typing), but if you are planning to do more, like this answer on converting a String to a List of Longs exemplifies, the streaming API is really powerful by allowing to pipeline your operations one after the other.
For the sake of, you know, completeness.
List<String> items= Stream.of(commaSeparated.split(","))
.map(String::trim)
.collect(toList());