Collections

Java ArrayList to comma separated string example

Java ArrayList to comma separated string example shows how to convert ArrayList to comma separated String in Java.

How to convert ArrayList to comma separated string in Java?

There are several ways using which you can convert ArrayList to comma separated string as given below.

1)    Convert ArrayList to comma separated string using StringBuilder

Iterate through the ArrayList elements and append them one by one to the StringBuilder followed by comma. At the end, convert StringBuilder to String object and remove last extra comma from String.

Example

Output

Note: if you are using Java version 1.4 or below, use StringBuffer instead of StringBuilder class.

2)    Using toString method of ArrayList

You can use toString method of ArrayList to convert it to comma separated string as given below.

Output

3)    Using join method of String class (Only Java 8 and above)

If you are using Java version 8 or above, you can use join method of String class to convert ArrayList to String.

It returns all elements of the specified list joined together with the delimiter specified.

Output

4)    Using Apache Commons StringUtils class

if you are using Apache Commons library, you can use join method of StringUtils class to convert ArrayList to comma separated string.

Output

Please let us know your views in the comments section below.

Join 1000+ fellow learners! Enter your email address below: