# Convert a comma-separated string to a list in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Jul 4, 2021, updated Oct 29, 2022. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-string-with-separator-to-list

> Learn about 3 different ways to convert a comma-separated string to a list in Java.

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

## Convert a comma-separated string to a list using core Java

The `String` class in Java provides [`split()` method](https://attacomsian.com/blog/java-string-split) to split a string into an array of strings. You can use this method to turn the comma-separated list into an array:

```java
String fruits = "?,?,?,?,?,?";

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

Next, use the `Arrays.asList()` method to [convert the array into a list](https://attacomsian.com/blog/java-convert-array-to-list#using-arraysaslist-method):

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

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

```java
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:

```java
String fruits = "?, ?, ?, ?, ?, ?";

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

## Convert a comma-separated string to a list using Java streams

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

```java
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:

```java
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 into an integer list.

## Convert a comma-separated string to a list using Apache Commons Lang

[Apache Commons Lang](https://commons.apache.org/proper/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:

```xml
<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:

```groovy
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:

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

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

```java
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](https://attacomsian.com/blog/java-convert-list-to-comma-separated-string)
