Convert List to Comma-Separated String in Java

This tutorial shows several ways for converting a List to a comma-separated String in Java.

1- Java 7

If you’re using Java 7 or older versions, then the common way of converting a List to a comma-separated String is through StringBuilder as the following:

2- Java 8

In Java 8, you can do the conversion in 2 ways:

2.1. String.join()

The most common way is through using the join() method provided by the String object.

2.2. Collectors.joining()

The other way is through converting the List into a Stream and then collecting it as a comma-separated String.

3- Apache Commons Lang

Apart from JDK, you can still convert a List to a comma-separated String using Apache Commons Lang library through its StringUtils utility class.

5 1 vote
Article Rating

Hussein Terek

Owner of programmergate.com, I have a passion for software engineering and everything related to Java environment.

guest
0 Comments
Inline Feedbacks
View all comments