This post will discuss how to convert a List to a comma-separated String in Java.

1. Using Apache Commons Lang

Apache Commons Lang library offers the StringUtils.join() method, which can concatenate elements of an iterable together using the specified separator.

Download Code

Output:

Hello,World

2. Using Guava

Similar to Apache Commons Lang, the Guava library offers a Joiner class to join elements of an iterable using a separator between consecutive elements.

Download Code

Output:

Hello,World

3. Using String.join() method

Since Java 8, you can use the String.join() method that works only for Iterable<String>. It joins strings together with a specified separator.

Download  Run Code

Output:

Hello,World

4. Stream API

From Java 8 onwards, this can be efficiently done using joining Collector:

Download  Run Code

Output:

Hello,World

5. Using StringBuilder

Finally, you can iterate through the list and append each value to a StringBuilder along with the specified delimiter. The following solution demonstrates this by creating a utility function that returns the string representation of the StringBuilder.

Download  Run Code

Output:

Hello,World

That’s all about converting a List to a comma-separated String in Java.