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 - In our first solution, we’ll convert a string to a list of strings and integers using core Java. First, we’ll split our string into an array of strings using split, a String class utility method.
Discussions

array - Java split a string into ArrayList - Code Review Stack Exchange
If you're able to change that code as well you can also take a look at alternatives. For example JSON/XML serialisation/deserialisation of an entire class. ... If you're sticking with vanilla Java (which I think is fine), then you're right that cleaning your String, splitting it, then converting ... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
August 3, 2017
regex - Splitting a string into a list in Java - Stack Overflow
Connect and share knowledge within ... and easy to search. Learn more about Teams ... Which isn't compiling. I'm not really adept at regex. Any help please. ... @lmiguelvargasf I can convert the string if the delimiter is just 1 comma, but not in this case, thus the question. ... Do you mean you want it split "whenever ... More on stackoverflow.com
🌐 stackoverflow.com
[Java] Splitting Strings and creating an ArrayList with the substrings
Arrays.asList(tokens) has the type List. You can't construct an ArrayList using a List as the only input to the constructor. More on reddit.com
🌐 r/learnprogramming
9
9
December 27, 2016
java - How to split a String to an ArrayList? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I know there is a String split method that returns an array but I need an ArrayList. I am getting input from a textfield (a list of numbers; e.g. More on stackoverflow.com
🌐 stackoverflow.com
🌐
BeginnersBook
beginnersbook.com › 2015 › 05 › java-string-to-arraylist-conversion
Java – How to Convert a String to ArrayList
September 20, 2022 - We are splitting the string based on comma (,) as delimiter to get substrings. These substrings are assigned to ArrayList as elements. import java.util.ArrayList; import java.util.List; import java.util.Arrays; public class JavaExample { public static void main(String args[]){ String num = "22,33,44,55,66,77"; String str[] = num.split(","); List<String> al = new ArrayList<String>(); al = Arrays.asList(str); for(String s: al){ System.out.println(s); } } } Output: 22 33 44 55 66 77 ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-a-string-to-arraylist-in-java
How to Convert a String to ArrayList in Java? - GeeksforGeeks
July 23, 2025 - // Java program to convert String ... = str.split(""); // Now convert string into ArrayList ArrayList<String> strList = new ArrayList<String>( Arrays.asList(strSplit)); // Now print the ArrayList for (String s : strList) ...
🌐
Programming.Guide
programming.guide › java › split-string-into-arraylist.html
Java: How to split a String into an ArrayList | Programming.Guide
Generating a random point within a circle (uniformly) Here are a few ways to split (or convert, or parse) a string of comma separated values: input = "aaa,bbb,ccc"; // To array String[] arr = input.split(","); // To fixed-size list List<String> l = Arrays.asList(input.split(",")); // To ArrayList ...
🌐
How to do in Java
howtodoinjava.com › home › string › comma-separated string to list in java (split and streams)
Comma-Separated String to List in Java (split and Streams)
3 weeks ago - To convert a comma-separated string to a list in Java, we call split() with a comma regex and pass the resulting array to Arrays.asList(), List.of() or new ArrayList<>(), depending on whether the list must be changeable.
🌐
Medium
medium.com › @AlexanderObregon › turning-a-string-into-a-list-of-words-in-java-c1e4bd85cad8
Turning a String into a List of Words in Java | Medium
July 28, 2025 - If you had a sentence with several tabs or double spaces, those in-between gaps still split into entries, and those blanks sneak through unless filtered. If you prefer a more compact way to write that filtering step, Java’s stream API lets you handle it in a single line while keeping things readable. Streams turn arrays or collections into pipelines, where each item flows through a series of operations. Here’s a version that matches the manual loop above: List<String> filteredWords = Arrays.stream(words) .map(String::trim) .filter(s -> !s.isEmpty()) .collect(Collectors.toList());
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:
🌐
LabEx
labex.io › tutorials › java-how-to-split-a-string-into-an-arraylist-using-a-delimiter-in-java-415655
How to split a string into an ArrayList using a delimiter in Java | LabEx
The split() method takes a regular expression as a parameter and returns an array of substrings: ... Let us modify our StringSplitDemo.java file to include code that splits a string.
🌐
Techie Delight
techiedelight.com › home › java › convert comma-separated string to list in java
Convert comma-separated String to List in Java | Techie Delight
July 7, 2026 - 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.
🌐
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
Well, Java doesn't provide any such constructor or factory method to create ArrayList from delimited String, but you can use String.split() method and Arrays.asList() method together to create an ArrayList from any delimited String, not just ...
🌐
W3Schools
w3schools.com › java › ref_string_split.asp
Java String split() Method
The split() method splits a string into an array of substrings using a regular expression as the separator. If a limit is specified, the returned array will not be longer than the limit.
🌐
IONOS
ionos.com › digital guide › websites › web development › string splitting in java
How to split strings in Java - IONOS
January 6, 2025 - In this example, a string variable x is ini­tial­ized. Then Java’s split() is used on the string. The parameter searches the string for spaces and splits it in the places where it finds them. The result is then saved in an array called “output”. The for loop is used to list the sub­strings.
🌐
Reddit
reddit.com › r/learnprogramming › [java] splitting strings and creating an arraylist with the substrings
r/learnprogramming on Reddit: [Java] Splitting Strings and creating an ArrayList with the substrings
December 27, 2016 -

I have a few classes in this program sending and returning values among themselves. This one reads a string from another class and has to split the string to substrings and return the strings broken into <Box> objects as an ArrayList.

I have tried multiple methods, but nothing seems to work.

public class Converter {
private String s;

public Converter(String s) {
	this.s = s;
}

public ArrayList<Box> convert() {

    String[] tokens = s.split(",");

    Box box1 = new Box(tokens[0]);
    Box box2 = new Box(tokens[1]);
    Box box3 = new Box(tokens[2]);
    Box box4 = new Box(tokens[3]);
    Box box5 = new Box(tokens[4]);
    Box box6 = new Box(tokens[5]);
    Box box7 = new Box(tokens[6]);
    Box box8 = new Box(tokens[7]);

    ArrayList<Box> list = new ArrayList<Box>(Arrays.asList(tokens));

    return list;
 }
}

I am getting “cannot resolve constructor ‘ArrayList(java.util.List<T>” at the Array.asList part.

Any suggestions?

🌐
Sentry
sentry.io › sentry answers › java › how to split a string in java
How to split a string in Java
September 23, 2026 - public class Main { public static void main(String[] arg) { String str = "java"; String[] arrOfStr = str.split(""); for (String a : arrOfStr) { System.out.println(a); } } } ... Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
🌐
How to do in Java
howtodoinjava.com › home › string › split a string in java: split(), pattern, scanner and more
Split a String in Java: split(), Pattern, Scanner and More
3 weeks ago - The splitAsStream() method, added in Java 8, returns the parts as a Stream<String>. That suits input we want to trim, filter or convert in the same pipeline, and the result can go straight into a list with the Java 16 method toList().
🌐
YouTube
youtube.com › watch
Splitting strings in Java (read from a file, split, and create an ArrayList of objects) - YouTube
This series of videos is suitable for programmers with some programming or Java experience who want to learn object-oriented programming and dig deeper into ...
Published: December 31, 2022