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
๐ŸŒ
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"; // step one : converting comma separate String to array of String String[] elements = csv.split(","); // step two : convert String array to list of String List<String> fixedLenghtList = Arrays.asList(elements); ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-convert-comma-seperated-java-string-to-an-array
How to convert comma seperated java string to an array.
public class Tester { public static void main(String[] args) { String text = "This,is,a,comma,seperated,string."; String[] array = text.split(","); for(String value:array) { System.out.print(value + " "); } } }
๐ŸŒ
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 - Weโ€™ll use the split method to convert our numbers string into an array of strings. Then, weโ€™ll convert each string in our new array to an integer and add it to our list:
๐ŸŒ
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 ... System.out.println("List = " + myList); // comma separated String str = String.join(", ", myList); System.out.println("String (Comma Separated) = " + str); } }...
๐ŸŒ
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.
๐ŸŒ
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:
Find elsewhere
๐ŸŒ
OneCompiler
onecompiler.com โ€บ questions โ€บ 3xmsrcbms โ€บ -java-how-to-convert-comma-separated-string-into-an-array
[Java] How to convert comma separated string into an array? - Questions - OneCompiler
Using .split() on a string we can convert that into an array. Following code shows how to do that. import java.util.Arrays; public class JavaArrays { public static void main(String[] args) { String inp = "red,green,blue"; String[] colors = inp.split(","); System.out.println(Arrays.toString...
๐ŸŒ
Make Community
community.make.com โ€บ questions
Convert a String containing commas as separators into an Array - Questions - Make Community
January 31, 2023 - I have a string of text โ€œ01pdf1,01sb1โ€. The comma in the text is a separator between two elements: โ€œ01pdf1โ€ and โ€œ01sb1โ€. I want to conduct a series of operations for each element. In order to do this, I think I should use the iterator module. In order to use the iterator, I need to convert this original text string into an array so that it can deal with each element separately.
Top answer
1 of 3
5

Basic improvements

  1. Instead of setting the size of the int array to 10, it would be better to derive the right size from the size of String array
  2. Instead of int number[] the more conventional way to write is int[] number
  3. For structures that contain multiple values it's more natural to give plural names, for example "number" -> "numbers" for an array of numbers
  4. The variable names are very poor in general, and should be improved to better reflect their purpose, making the code easier to understand

Something like this:

String line = "1,2,3,1,2,2,1,2,3,";
String[] parts = line.split(",");
int[] ints = new int[parts.length];
for (int i = 0; i < parts.length; i++) {
    ints[i] = Integer.parseInt(parts[i]);
}

Split to logical steps

It's good to get into the habit of decomposing tasks to their small steps. That is, instead of having all the logical steps in a single main method, it would be better to split to multiple functions, for example:

static int[] toIntArray(String[] arr) {
    int[] ints = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        ints[i] = Integer.parseInt(arr[i]);
    }
    return ints;
}

static int[] parseLineToIntArray(String line) {
    return toIntArray(line.split(","));
}

public static void main(String[] args) {
    String line = "1,2,3,1,2,2,1,2,3,";
    System.out.println(Arrays.toString(parseLineToIntArray(line)));
}
2 of 3
3

You code is not properly indented and IMO your comments don't add any value. You could create a separate function instead of putting everything in the main function. Also you could add an extra parameter so you can specify the delimiter instead of always being ",".

Also if you can use java 8 this becomes even more trivial:

public static int[] toIntArray(String input, String delimiter) {

   return  Arrays.stream(input.split(delimiter))
                 .mapToInt(Integer::parseInt)
                 .toArray();
}
๐ŸŒ
Wyzant
wyzant.com โ€บ resources โ€บ ask an expert
How to convert comma-separated String to ArrayList? | Wyzant Ask An Expert
March 16, 2019 - 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?
๐ŸŒ
Adobe
experienceleague.adobe.com โ€บ en โ€บ docs โ€บ experience-manager-learn โ€บ forms โ€บ adaptive-forms โ€บ converting-comma-seperated-string-to-array
Converting comma separated string to array of strings in AEM Forms Workflow | Adobe Experience Manager
The custom code in the process step converts the submitted data in to the correct format. We pass the JSON object path and the element name to the process step. The code in the process step replaces the comma separated values of the element into an array of strings. ... Make sure the Data File path in the Adaptive Formโ€™s submission options is set to โ€œData.xmlโ€. This is because the code in the process step looks for a file called Data.xml under the payload folder. import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.InputStreamRead
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2016 โ€บ 03 โ€บ how-to-convert-array-to-comma-separated-string-in-java.html
How to Convert an Array to Comma Separated String in Java - Example Tutorial
June 22, 2025 - The best option was to store comma-separated values and then I looked into JDK API for any method which can do that, there wasn't any. So, I quickly wrote one as seen in the first example. Later, I did a little bit of research and come up with four different ways to convert a String array to comma separated String in Java.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 09 โ€บ convert-comma-separated-string-to-hashset-in-java
Convert Comma Separated String to HashSet in Java
September 22, 2022 - Split the given comma separated to substrings using split() method. Store the returned string elements in a String array. Pass this array to a List by using Arrays.asList() method which converts string array to a List. Convert the List obtained in previous step to HashSet by passing it in the ...
๐ŸŒ
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 - Java examples to show you how to convert a comma-separated String into a List and vice versa. ... 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); } }