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>.

Answer from AlexR on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ convert a comma separated string to a list in java
Convert a Comma Separated String to a List in Java
September 3, 2025 - Even though both these functions split the string into an array, the splitPreserveAllTokens preserves all tokens including the empty strings created by adjoining separators, while the split method ignores the empty strings. So, if we have empty strings that we want to be included in our list, then we should use the splitPreserveAllTokens instead of split. Finally, weโ€™ll use the Guava library to convert our strings to their appropriate lists. To convert our countries string, weโ€™ll first call Splitter.on with a comma as the parameter to specify what character our string should be split on.
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java โ€“ convert comma-separated string to a list
Java - Convert comma-separated String to a List - Mkyong.com
April 14, 2017 - package com.mkyong.utils; import java.util.Arrays; import java.util.List; public class TestApp1 { public static void main(String[] args) { String alpha = "A, B, C, D"; //Remove whitespace and split by comma List<String> result = Arrays.asList(alpha.split("\\s*,\\s*")); System.out.println(result); } } ... package com.mkyong.utils; import java.util.Arrays; import java.util.List; public class TestApp2 { public static void main(String[] args) { List<String> list = Arrays.asList("A", "B", "C", "D"); String result = String.join(",", list); System.out.println(result); } }
๐ŸŒ
Initial Commit
initialcommit.com โ€บ blog โ€บ java-convert-comma-separated-string-to-list
Java โ€“ Convert comma-separated String to List - Initial Commit
With Java 7 and older versions, ... result; } In Java 8, you can split the String by the comma โ€œ,โ€ delimiter and then use Arrays.stream() and collect() methods to generate a List....
๐ŸŒ
Blogger
javahungry.blogspot.com โ€บ 2023 โ€บ 04 โ€บ comma-separated-string-to-list.html
Convert Comma-Separated String to List in Java [3 ways] | Java Hungry
import java.util.List; import ...lect(Collectors.toList()); System.out.println(result); } } Output: [Alive, is, Awesome] Using the String class split() method and Arrays.asList() method we can convert comma-separated string to the List as shown in the below exampl...
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 09 โ€บ convert-comma-separated-string-to-hashset-in-java
Convert Comma Separated String to HashSet in Java
September 22, 2022 - import java.util.Arrays; import ... " + hSet); } } Output: Java 8 introduced a new concept of streams. You can use Java 8 stream to convert the given comma separated string to HashSet....
Find elsewhere
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ java-string-with-separator-to-list
Convert a comma-separated string to a list in Java
October 29, 2022 - If the comma-separated string contains white spaces, you can pass a regular expression to split() to remove them: String fruits = "?, ?, ?, ?, ?, ?"; List<String> fruitsList = Arrays.asList(fruits.split("\\s*,\\s*")); Java Stream API can also be used to convert to comma-separated string into a list, as shown below:
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ convert a list to a comma-separated string
Convert a List to a Comma-Separated String | Baeldung
July 2, 2025 - List conversion is still a hot topic as itโ€™s something that we do very often as Java developers. In this tutorial, weโ€™ll learn how to convert a List of String to a comma-separated String using four different approaches. Weโ€™ll use three different classes and their methods, available since Java 8, for conversion.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-convert-a-comma-separated-string-into-an-arraylist-in-java
Convert String into comma separated List in Java
July 30, 2019 - import java.util.*; public class Demo { public static void main(String args[]) { List<String> myList = new ArrayList<>(Arrays.asList("One", "Two", "Three", "Four")); System.out.println("List = " + myList); // comma separated String str = String.join(", ", myList); System.out.println("String (Comma Separated) = " + str); } } List = [One, Two, Three, Four] Comma separated String: One, Two, Three, Four ยท
๐ŸŒ
Java67
java67.com โ€บ 2017 โ€บ 09 โ€บ how-to-convert-comma-separated-string-to-ArrayList-in-java-example.html
How to Convert a Comma Separated String to an ArrayList in Java - Example Tutorial | Java67
String csv = "Apple, Google, Samsung"; ... String List<String> fixedLenghtList = Arrays.asList(elements); // step three : copy fixed list to an ArrayList ArrayList<String> listOfString = new ArrayList<String>(fixedLenghtList); System.out.println("list from comma separated String : " + listOfString); ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-convert-a-comma-separated-string-into-an-arraylist-in-java
How to Convert a Comma-Separated String into an ArrayList in Java? - GeeksforGeeks
November 16, 2024 - In Java, to convert a comma-separated string into ArrayList, we can use the split() method to break the string into an array based on the comma delimiter. Another method we can also use Java Streams for a functional approach to achieve the same ...
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ how-to-convert-comma-separated-string-to-list
How to Convert Comma-Separated String to List in Java: Built-in Methods vs Custom Code โ€” javathinking.com
No Empty Tokens: By default, StringTokenizer skips empty tokens. For example, ",,apple,,banana" โ†’ ["apple", "banana"] (empty tokens are ignored). No Trimming: Does not trim whitespace. Legacy API: Not recommended for new code; prefer split() or streams instead. Java 8 introduced Pattern.splitAsStream(), which splits a string into a stream of substrings, making it easy to collect into a list.
๐ŸŒ
Simplesolution
simplesolution.dev โ€บ java-convert-comma-separated-string-to-list
Java Convert Comma Separated String to List - Simple Solution
*/ public static List<String> commaSeparatedStringToList(String commaSeparatedString) { if(commaSeparatedString == null) { return null; } String[] values = commaSeparatedString.split(","); return Arrays.asList(values); } } In the following example Java code, we learn how to use the above utility class to convert a comma separated String into a List of Strings in Java program.
๐ŸŒ
Iditect
iditect.com โ€บ program-example โ€บ java--how-to-convert-commaseparated-string-to-list.html
java - How to convert comma-separated String to List?
To convert a comma-separated String into a List<String> in Java, you can use the split method to split the String at each comma and then convert the array of Strings into a List.
๐ŸŒ
Medium
biniamasnake.medium.com โ€บ a-one-liner-code-to-convert-comma-separated-strings-to-list-in-java-a80106493a28
A one-liner code to convert comma-separated Strings to List in Java - Biniam Asnake - Medium
March 1, 2021 - // String that is comma separated String listOfUserIds = โ€œ10,11,12,13โ€;// The magic that converts it to list List<String> userIds = new ArrayList<String>(Arrays.asList(listOfUserIds.split(โ€œ,โ€))); To convert the List back to String: ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ convert-string-into-comma-separated-list-in-java
Convert String into comma separated List in Java - GeeksforGeeks
December 11, 2018 - // Java program to convert String // to comma separated List import java.util.*; public class GFG { public static void main(String args[]) { // Get the String String string = "Geeks For Geeks"; // Print the String System.out.println("String: " + string); // convert String to array of String String[] elements = string.split(" "); // Convert String array to List of String // This List is modifiable List<String> list = new ArrayList<String>( Arrays.asList(elements)); // Print the comma separated List System.out.println("Comma separated List: " + list); } }
๐ŸŒ
Java67
java67.com โ€บ 2018 โ€บ 10 โ€บ how-to-convert-list-or-set-of-string-to-comma-separated-string-in-java-csv.html
How to Convert a List of String to Comma Separated String (CSV) in Java 8? Example | Java67
JDK 8 has provided a utility class called StringJoiner as well as added a join() method into String class to convert a List, Set, Collection, or even array of String objects to a comma-separated String in Java.