Java – Convert list to string

In this article, we will look at different methods to convert a list to string in java. The list elements in string can be comma separated or with some other delimiter.
All the methods in this article can also convert an ArrayList to string in java.
Following is a list of methods for quick reference

Method 1: List to string with join()
In java 8, join() method was added to String class. It is a static method which takes 2 arguments:
* First is the delimiter or separator between list elements.
* Second is an object that implements Iterable interface.

List implements Iterable, hence it can be passed to join(). Example,

List<String> list = List.of("A", "B", "C");
String s = String.join(",", list);
System.out.println(s); // prints A,B,C

This method:
A. Can only be used to convert list of string values to a string.
B. Can be used to convert list to string with delimiter such as a comma.
Method 2: List to string with java 8 stream
Java 8 stream can be used to operate over the elements of a list one by one. Create a stream with stream() method and invoke its map() method.
map() will be invoked for each list element. Convert each element to a string with toString() method.

Finally, accumulate these elements to a string using collect() method, that takes a Collector argument. Example,

List<Integer> numbers = List.of(1, 2, 3);
String s = numbers.
           stream().
           map(Object::toString).
           collect(Collectors.joining());
System.out.println(s); // prints 123

If you want to separate the list elements with a delimiter, then supply it as an argument to joining() method. There is an overloaded version of joining() in Collectors class.

Note that this method can be used to convert a list of integers to a string or a list of strings to a string.

Method 3: List to string with Apache Commons
Apache Commons library has a StringUtils class with join() method. It accepts a List as argument and converts it to a string.
This string is a combination of list elements separated with a comma and it is enclosed between square brackets. Example,

List<String> list = List.of("A", "B", "C");
String str = StringUtils.join(list);
System.out.println(str); // prints [A, B, C]

The type of list expected by join() is String. So, this method can only be used to convert a list of string values to a string.

There is overloaded version of join() which also takes a separator or delimiter as second argument. This should be used when you do not want the list elements to be separated by a comma, but some other separator.
It also removes the surrounding square brackets from the resultant string.

Also, there are more overloaded versions of join(), which can convert a string array to string.

To add Apache Commons library to your project, add below dependency as per your build tool.
Maven

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.12.0</version>
</dependency>

Gradle

implementation 'org.apache.commons:commons-lang3:3.12.0'

Method 4: Using Guava library
Guava library has a Joiner class, which can be used to convert a list to a string.
Joiner class has a join() method which takes an Iterable as argument and returns a string created with the elements of iterable.

To create an object of Joiner, use its static on() method.
on() takes a string argument, which acts as the separator between list elements. Example,

List<Integer> numbers = List.of(1, 2, 3);
String join = Joiner.on(":").join(numbers);
System.out.println(join);

This method can be used to convert a list of integers to a string with a custom separator.

To add Guava library to your project, add below dependency as per your build tool.

Maven

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>30.1.1-jre</version>
</dependency>

Gradle

implementation 'com.google.guava:guava:30.1.1-jre'
Method 5: List to string with Arrays
java.util.Arrays class has a toString() method, which takes an array argument and converts it to a string.
This string contains the elements of list separated by a comma and enclosed between square brackets.

Now, we need to convert the list to an array so that it can be supplied to toString() method. A list can be converted to an array with its toArray() method. Example,

List<Integer> numbers = List.of(1, 2, 3);
String s = Arrays.toString(numbers.toArray());
System.out.println(string); // prints [1, 2, 3]

With this method a list of integers can be converted to a string separated with comma, but we cannot change the separator.

If you need to remove the square brackets around the string, then use replaceAll() method to replace square brackets with empty string, as shown below

List<Integer> numbers = List.of(1, 2, 3); 
String s = Arrays.toString(numbers.toArray()); 
s = s.replaceAll("\\[|\\]", "");
System.out.println(s); // prints 1, 2, 3

Method 6: List to string with iteration
Iterate over the list using a loop. Add current loop element to a string with StringJoiner class.

StringJoiner class was added in java 8 and it has add() method which adds a string to the resultant string. Example,

List<Integer> numbers = List.of(1, 2, 3);
StringJoiner joiner = new StringJoiner(",");
for (Integer number : numbers) {
   joiner.add(number.toString());
}
System.out.println(joiner.toString()); // prints 1,2,3

With this method, you can convert a list to string with separator and without surrounding brackets.

In this article, we looked at 6 ways to convert a list to string in java along with methods introduced in java 8.
Hope the article was useful.