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.

 

Hussein Terek

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

You may also like...

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
CarlosRib Recent comment authors
newest oldest most voted
CarlosRib
Guest
CarlosRib

Hi, here on the forum guys advised a cool Dating site, be sure to register – you will not REGRET it https://bit.ly/2VHWPjt