HOWTO · Java
How to Convert an Arraylist to a String in Java
Learn how to convert an ArrayList to a String in Java with a chosen delimiter or a bracketed representation.
On this page
To convert an ArrayList to a String, choose whether you need one value per element with a delimiter or a bracketed representation for display. For delimiter-joined text, String.join() is a direct option; StringBuilder is useful when you need to customize the loop. The examples below use Apple, Orange, and Mango as the list values.
Convert ArrayList to String With + Operator in Java
The + operator concatenates strings. For a short, fixed list, you can concatenate its values directly and place separators between them. Repeated concatenation inside a loop creates intermediate strings, so use StringBuilder for larger lists.
import java.util.ArrayList;
import java.util.List;
public class SimpleTesting {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Mango");
String joined = fruits.get(0) + "," + fruits.get(1) + "," + fruits.get(2);
System.out.println(joined);
}
}
Convert ArrayList to String Using the append() Method in Java
Use StringBuilder.append() to build a result in a loop. Add the delimiter before each value except the first; this avoids leaving an extra separator at the end. Calling toString() returns the completed String.
import java.util.ArrayList;
import java.util.List;
public class SimpleTesting {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Mango");
StringBuilder result = new StringBuilder();
for (String fruit : fruits) {
if (result.length() > 0) {
result.append(", ");
}
result.append(fruit);
}
String joined = result.toString();
System.out.println(joined);
}
}
Convert ArrayList to String Using the join() Method in Java
String.join() combines the elements using the delimiter you supply. The separator appears between adjacent values, and is not added before the first value or after the last one. This API is discussed here without specifying a minimum Java version.
import java.util.ArrayList;
import java.util.List;
public class SimpleTesting {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Mango");
String joined = String.join(", ", fruits);
System.out.println(joined);
}
}
Convert ArrayList to String Using StringUtils Class in Java
If your project already uses Apache Commons Lang, StringUtils.join(fruits, ", ") joins the values with the supplied separator. This option requires the Commons Lang dependency on the project classpath; it is not part of the Java standard library. See the StringUtils class guide for more about the library.
Convert ArrayList to String Using the replaceAll() Method in Java
You can remove the brackets and separator spacing from List.toString() with replaceAll(), but this is a formatting shortcut. It relies on the list’s display representation, so values containing commas or brackets can make the result ambiguous. This example removes only the square brackets; it leaves the comma-and-space separators from List.toString() unchanged.
import java.util.ArrayList;
import java.util.List;
public class SimpleTesting {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Mango");
String joined = fruits.toString().replaceAll("\\[|\\]", "");
System.out.println(joined);
}
}
Convert ArrayList to String Using the joining() Method in Java
For a stream-based approach, call stream() on the list and collect its elements with Collectors.joining(). The delimiter is inserted between values. This API is discussed without making a minimum Java-version claim.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class SimpleTesting {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Mango");
String joined = fruits.stream().collect(Collectors.joining(", "));
System.out.println(joined);
}
}
Convert ArrayList to String Using the deepToString() Method in Java
Arrays.deepToString() creates a bracketed array-style representation, which is useful for display and nested array values. It does not create a plain delimiter-joined string. Convert the list to an array before passing it to the method.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SimpleTesting {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Mango");
String representation = Arrays.deepToString(fruits.toArray());
System.out.println(representation);
}
}