Skip to content

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. The example also shows different ways to do the same.

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

First, iterate through the ArrayList elements and append them one by one to the StringBuilder followed by a comma. In the end, convert StringBuilder to String object and remove the 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 the toString method of ArrayList

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

Output

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

If you are using Java version 8 or above, you can use the 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 the join method of StringUtils class to convert ArrayList to comma separated string.

Output

Refer to covert comma separated String to ArrayList example as well.

This example is a part of the Java ArrayList tutorial with examples.

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

About the author

Leave a Reply

Your email address will not be published.