Java – Convert List to comma-separated String

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

1- Java 7

The traditional way of converting a List to a comma-separated String in Java 7 and older versions is through using StringBuilder as the following:

2- Java 8

In Java 8, there are 2 conversion 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 less common 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.

 

husseinterek

Founder of programmergate.com, I have a passion in software engineering and everything related to java environment.

You may also like...

Leave a Reply

avatar