Convert List to String Java example shows how to convert list to string in Java. It also shows how to convert ArrayList to string & LinkedList to string using various approaches.
How to convert List to String?
Let’s first create a List using the ArrayList class as given below.
1 2 3 4 5 6 7 |
List<String> listDays = new ArrayList<String>(); listDays.add("Sunday"); listDays.add("Monday"); listDays.add("Tuesday"); listDays.add("Wednesday"); listDays.add("Thursday"); |
There are several ways using which you can convert List (ArrayList or LinkedList) to a string containing all the list elements.
1. Convert List to String Using toString method of List
The List provides the toString
method which prints all the List elements in the following format.
1 |
[element1, element2, element3…elementN] |
1 2 |
String strListString = listDays.toString(); System.out.println(strListString); |
Output
1 |
[Sunday, Monday, Tuesday, Wednesday, Thursday] |
The separator used by the toString
method is “, ” to separate the the List elements. Also the list elements are wrapped inside the square brackets ([]).
If you want another separator you can replace the “, ” with the separator you want (such as tab “\t” or blank space ” “) as given below.
1 2 3 4 5 6 |
String strListString = listDays.toString(); //replace ", " with " " space strListString = strListString.replaceAll(", ", " "); System.out.println(strListString); |
Output
1 |
[Sunday Monday Tuesday Wednesday Thursday] |
If you want to remove the square brackets too, you can use following code.
1 2 3 4 5 6 |
String strListString = listDays.toString(); //replace "[" and "]" with empty string "" strListString = strListString.replaceAll("\\[|\\]", ""); System.out.println(strListString); |
Output
1 |
Sunday, Monday, Tuesday, Wednesday, Thursday |
Here, we have used regular expression pattern “\\[|\\]” to find the square brackets “[” and “]” in the string. Since the “[” and “]” are metacharacters in the a regular expression, we have to escape them with the “\\”. The “|” denotes “or” in regular expression. So basically we are finding “[” or “]” in the string and replacing all of them with an empty string.
Finally, you can change the separator and remove brackets in one statement as given below.
1 2 3 4 5 6 |
String strListString = listDays.toString(); strListString = strListString.replaceAll(", ", " ").replaceAll("\\[|\\]", ""); System.out.println(strListString); |
Output
1 |
Sunday Monday Tuesday Wednesday Thursday |
2. Using for loop
You can also use a for loop to loop through each element of a List and generate the string as given below.
1 2 3 4 5 6 7 8 9 10 11 12 |
StringBuilder sbListString = new StringBuilder(); String strSeparator = ","; //iterate the List for(String strDay : listDays){ sbListString.append(strDay); sbListString.append(strSeparator); } System.out.println(sbListString.toString()); |
Output
1 |
Sunday,Monday,Tuesday,Wednesday,Thursday, |
Notice the last the “,” after the last element in the output. You can remove it if you want using below given code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
StringBuilder sbListString = new StringBuilder(); String strSeparator = ","; //iterate the List for(String strDay : listDays){ sbListString.append(strDay); sbListString.append(strSeparator); } String strListString = sbListString.toString(); //remove last separator if(listDays.size() > 0) strListString = sbListString.substring(0, sbListString.length() - 1); System.out.println(strListString); |
Output
1 |
Sunday,Monday,Tuesday,Wednesday,Thursday |
Note: If you are using Java 1.4 or earlier version, use the StringBuffer instead of the StringBuilder class.
3. Using Apache Commons
If you are using the Apache Commons library, the StringUtils class provides the join
method which creates a string from the List elements. You can also provide a separator to the join
method as given below.
1 2 3 |
String strListString = StringUtils.join(listDays, ","); System.out.println(strListString); |
Output
1 |
Sunday,Monday,Tuesday,Wednesday,Thursday |
4. Java 8
Finally, if you are using Java 8 or later version, you can use the join
method of the String class to join the List elements and convert it to a string as given below. You can provide separator of your choice to the join
method.
1 2 |
String strListString = String.join(", ", listDays); System.out.println(strListString); |
Output
1 |
Sunday, Monday, Tuesday, Wednesday, Thursday |
This example is a part of the Java String tutorial and Java ArrayList tutorial.
Please let me know your views in the comments section below.