Convert comma-separated String to List in Java
This post will discuss how to convert comma-separated String to list in Java.
1. Using Arrays.asList with split() method
The idea is to split the string using the split() method and pass the resultant array into the Arrays.asList() method, which returns a fixed-size List backed by an array. To get a mutable ArrayList, we can further pass the fixed-size list to the ArrayList constructor.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { String str = "A,B,C,D"; List<String> list = Arrays.asList(str.split(",")); System.out.println(list); // [A, B, C, D] } } |
2. Using Guava Library
Another good alternative is to use the Splitter class from the Guava library to split the string, as shown below. This returns an Iterable instead of an array, which we can pass to Lists.newArrayList(). It creates a mutable ArrayList instance containing all elements of the Iterable. This method is preferred if we need to add or remove elements later, or some of the elements can be null.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.base.Splitter; import com.google.common.collect.Lists; import java.util.List; class Main { public static void main(String[] args) { String str = "A,B,C,D"; List<String> list = Lists.newArrayList(Splitter.on(",").split(str)); System.out.println(list); // [A, B, C, D] } } |
3. Using Java 8
From Java 8 onward, we can use Stream. The idea is to split the string using the split() method, convert the array into the stream, and then collect all the input elements into a new list using the Collectors.toList() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { String str = "A,B,C,D"; List<String> list = Stream.of(str.split(",")) .collect(Collectors.toList()); System.out.println(list); // [A, B, C, D] } } |
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.ArrayList; import java.util.List; class Main { public static void main(String[] args) { String str = "A,B,C,D"; List<String> list = new ArrayList<>(); for (String s : str.split(",")) { list.add(s); } System.out.println(list); // [A, B, C, D] } } |
Note that Collectors.toList() doesn’t offer any guarantee on the type of list returned. To get the desired Collection, we can use toCollection() method provided by the Collectors class that can accept desired constructor method reference like ArrayList::new or LinkedList::new.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { String str = "A,B,C,D"; List<String> list = Stream.of(str.split(",")) .collect(Collectors.toCollection(ArrayList::new)); System.out.println(list); // [A, B, C, D] } } |
4. Using splitAsStream() method
We can also use the splitAsStream() method, which returns the stream computed by splitting the input around matches of the given pattern.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.List; import java.util.regex.Pattern; import java.util.stream.Collectors; class Main { public static void main(String[] args) { String str = "A,B,C,D"; List<String> list = Pattern.compile(",").splitAsStream(str) .collect(Collectors.toList()); System.out.println(list); // [A, B, C, D] } } |
That’s all about converting comma-separated String to List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)