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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class ArrayListToCommaSeparatedStringExample { public static void main(String[] args) { //Create an ArrayList ArrayList<String> aListLanguages = new ArrayList<String>(); //Add elements to ArrayList aListLanguages.add("English"); aListLanguages.add("French"); aListLanguages.add("Spanish"); aListLanguages.add("Hindi"); aListLanguages.add("Arabic"); aListLanguages.add("Russian"); StringBuilder sbString = new StringBuilder(""); //iterate through ArrayList for(String language : aListLanguages){ //append ArrayList element followed by comma sbString.append(language).append(","); } //convert StringBuffer to String String strList = sbString.toString(); //remove last comma from String if you want if( strList.length() > 0 ) strList = strList.substring(0, strList.length() - 1); System.out.println(strList); } } |
Output
1 |
English,French,Spanish,Hindi,Arabic,Russian |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* * toString method returns string in below format * [element1, element2, element3....] * * Just replace '[', ']' and spaces with empty strings * to have comma separated values. * */ String strList = aListLanguages.toString(); strList = strList.replace("[", "") .replace("]", "") .replace(" ", ""); System.out.println(strList); |
Output
1 |
English,French,Spanish,Hindi,Arabic,Russian |
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.
1 |
static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) |
It returns all elements of the specified list joined together with the delimiter specified.
1 2 |
String strList = String.join(“,”, aListLanguages); System.out.println(strListString); |
Output
1 |
English,French,Spanish,Hindi,Arabic,Russian |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
/* * Use join method of StringUtils class. * * static String join(Iterable<?> iterable, String separator) * * This method returns a single string containing elements of Iterable * joined with the specified separator. * */ String strList = StringUtils.join(aListLanguages, ","); System.out.println(strList); |
Output
1 |
English,French,Spanish,Hindi,Arabic,Russian |
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.