There are multiple ways to convert a comma-separated string into a list in Java. In this article, we'll look at 3 different methods to convert a string with a separator to a list.

Core Java

The String class in Java provides split() method to split a string into an array of strings. You can use this method to turn the comma-separated list into an array:

String fruits = "🍇,🍓,🍑,🥭,🍍,🥑";

String [] fruitsArray = fruits.split(",");

Next, use the Arrays.asList() method to convert the array into a list:

List<String> fruitsList = Arrays.asList(fruitsArray);

That's it. Here is the complete example code:

String fruits = "🍇,🍓,🍑,🥭,🍍,🥑";

List<String> fruitsList = Arrays.asList(fruits.split(","));

System.out.println(fruitsList);
// [🍇, 🍓, 🍑, 🥭, 🍍, 🥑]

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 Streams

Java Stream API can also be used to convert to comma-separated string into a list as shown below:

String fruits = "🍇, 🍓, 🍑, 🥭, 🍍, 🥑";

List<String> fruitsList = Stream.of(fruits.split("\\s*,\\s*"))
        .collect(Collectors.toList());
				
System.out.println(fruitsList);
// [🍇, 🍓, 🍑, 🥭, 🍍, 🥑]

In the above example, we first used the split() method to convert our fruits string into an array of strings. Then, we used the Stream class to convert the array into a list of strings.

An additional benefit of using Java Stream API is that you can perform other operations on the elements of the array before converting them into a list.

Look at the following example that converts a string of numbers into a list of integers using a stream:

String numbers = "23, 45, 2, 7, 99, 6";

List<Integer> list = Stream.of(numbers.split(","))
        .map(String::trim)
        .map(Integer::parseInt)
        .collect(Collectors.toList());

System.out.println(list);
// [23, 45, 2, 7, 99, 6]

The first part of the example is the same, convert a comma-separated string of numbers into an array.

Then, it trims the leading and trailing spaces from each string on the stream using the map(String::trim) method.

Next, the map(Integer::parseInt) method is called on our stream to convert every string to an Integer.

Finally, it calls the collect(Collectors.toList()) method on the stream to transform it to an integer list.

Apache Commons Lang

Apache Commons Lang is an open-source library that provides many utility classes to manipulate core Java classes.

One such utility class is the StringUtils that offers utility methods for string operations.

To add Commons Lang to your Maven project, add the following dependency to the pom.xml file:

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.12.0</version>
</dependency>

For Gradle, add the below dependency to your build.gradle file:

implementation 'org.apache.commons:commons-lang3:12.0'

Now you can use the StringUtils.splitPreserveAllTokens() method to convert the string into an array of strings:

String[] fruitsArray =  StringUtils.splitPreserveAllTokens(fruits, ",");

Next, use the Arrays.asList() method to transform the array into a list:

List<String> fruitsList = Arrays.asList(fruitsArray);

Both split() and splitPreserveAllTokens() methods split the string into an array of strings using a delimiter. However, the splitPreserveAllTokens() method preserves all tokens, including the empty strings created by adjoining separators, while the split() method ignores empty strings.

Read Next: Convert a list to a comma-separated string in Java

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.