Programming
String Manipulation
Data Conversion
Java
Coding Tutorial

How to convert comma-separated String to List?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Java, converting a comma-separated string into a List<String> usually starts with String.split(","). The real details are what happen next: trimming whitespace, handling empty values, deciding whether the list should be mutable, and recognizing when the input is actually CSV rather than a simple delimiter-separated string.

The simplest conversion

For a plain comma-separated string with no extra spaces:

java
1import java.util.Arrays;
2import java.util.List;
3
4public class Main {
5    public static void main(String[] args) {
6        String input = "apple,banana,cherry";
7        List<String> items = Arrays.asList(input.split(","));
8        System.out.println(items);
9    }
10}

This works, but the returned list is backed by the array from split, so it has a fixed size. You can replace elements, but you cannot add or remove items.

Trim whitespace and build a cleaner list

Real strings often look like:

text
apple, banana, cherry

If you split directly, the spaces remain. A more realistic version is:

java
1import java.util.Arrays;
2import java.util.List;
3import java.util.stream.Collectors;
4
5public class Main {
6    public static void main(String[] args) {
7        String input = "apple, banana, cherry";
8
9        List<String> items = Arrays.stream(input.split(","))
10                .map(String::trim)
11                .collect(Collectors.toList());
12
13        System.out.println(items);
14    }
15}

This gives you a normal mutable ArrayList created by the collector and removes the extra whitespace around each value.

Handle empty items explicitly

Sometimes the string contains missing values:

text
apple,,cherry,

Default split(",") behavior can be surprising because trailing empty strings are discarded unless you pass a limit argument.

To preserve them:

java
String input = "apple,,cherry,";
List<String> items = Arrays.asList(input.split(",", -1));
System.out.println(items);

Using -1 tells Java to keep trailing empty strings too. That matters when empty positions are meaningful, such as configuration slots or imported data columns.

If you instead want to discard empty results, filter them:

java
1List<String> items = Arrays.stream(input.split(",", -1))
2        .map(String::trim)
3        .filter(s -> !s.isEmpty())
4        .collect(Collectors.toList());

Know when this is not really CSV

A lot of inputs described as "comma-separated strings" are actually CSV-like data. Once quoted commas appear, plain split(",") is no longer correct.

For example:

text
apple,"banana,mango",cherry

Naive splitting produces the wrong pieces because the comma inside quotes is data, not a separator.

For real CSV, use a parser library instead of hand-rolled splitting. In Java, that usually means a dedicated CSV library rather than more complex regex unless the format is extremely controlled.

Mutability and API choice matter

Different conversions create different kinds of lists:

  • 'Arrays.asList(...) gives a fixed-size list'
  • 'List.of(...) gives an immutable list, but it is less direct for split results'
  • 'Collectors.toList() usually gives you a mutable list'

So the right conversion depends on what you want to do afterward. If the code will later append values, avoid a fixed-size list created only for convenience.

Common Pitfalls

The biggest mistake is forgetting to trim spaces. That leads to values like " banana" instead of "banana", which is especially annoying in comparisons and lookups.

Another mistake is assuming split(",") preserves trailing empty items. It does not unless you use the overload with a negative limit.

Developers also treat CSV as if it were the same as "string separated by commas." Once quoted fields enter the picture, simple splitting is no longer reliable.

Finally, do not ignore the list type you are producing. Arrays.asList is often good enough, but not when later code expects to add or remove elements.

Summary

  • For simple input, input.split(",") is the basic conversion step in Java.
  • Trim whitespace if the string may contain spaces after commas.
  • Use split(",", -1) when trailing empty values matter.
  • Choose a mutable or fixed-size list intentionally.
  • If the input is real CSV with quoted commas, use a proper CSV parser instead of naive splitting.

Course illustration
Course illustration

All Rights Reserved.