1. Introduction
In this quick tutorial, weβll explain how to convert a List of elements to a String. This can be useful in certain scenarios, like printing the contents to the console in a human-readable form for inspection/debugging.
2. Standard toString() on a List
One of the simplest ways is to call the toString() method on the List:
@Test
public void whenListToString_thenPrintDefault() {
List<Integer> intLIst = Arrays.asList(1, 2, 3);
System.out.println(intLIst);
}
Output:
[1, 2, 3]
This technique internally utilizes the toString() method of the type of elements within the List. In our case, weβre using the Integer type, which has a proper implementation of the toString() method.
If weβre using our custom type, such as Person, then we need to make sure that the Person class overrides the toString() method and doesnβt rely on the default implementation. If we donβt properly implement the toString() method, we might get unexpected results:
[org.baeldung.java.lists.ListToSTring$Person@1edf1c96,
org.baeldung.java.lists.ListToSTring$Person@368102c8,
org.baeldung.java.lists.ListToSTring$Person@6996db8]
3. Using String.join()
The String.join() method allows us to concatenate or join multiple String values together with a specified delimiter. Itβs a convenient way to combine a collection of Strings into a single String with a custom delimiter between each element.
Letβs first look at how the method joins a String List:
@Test
public void whenStringJoinWithStringList_thenPrintCustom() {
List<String> strList = Arrays.asList("one", "two", "three");
System.out.println(String.join(" : ", strList));
}
Output:
one : two : three
If our elements in the List arenβt subclasses of CharSequence, we can first convert the List to a List<String>, and then pass it to String.join():
@Test
public void whenStringJoinWithNonStringList_thenPrintCustom() {
List<Integer> intList = Arrays.asList(1, 2, 3);
List<String> strList = intList.stream().map(String::valueOf).collect(Collectors.toList());
System.out.println(String.join(" : ", strList));
}
Output:
1 : 2 : 3
In this example, we use Stream.map() and collect() to convert the List<Integer> to a List<String>. Then, we can pass the converted List to String.join() to get the expected output.
In fact, we can simplify this logic by using Collector.joining() without calling String.join() to achieve that. Next, letβs look at how itβs done.
4. Custom Implementation Using Collectors
Often, we might need to display the output in a different format.
Compared to the previous example, letβs replace the comma (,) with a hyphen (-), and the square brackets ([, ]) with a set of curly braces ({, }):
@Test
public void whenCollectorsJoining_thenPrintCustom() {
List<Integer> intList = Arrays.asList(1, 2, 3);
String result = intList.stream()
.map(String::valueOf)
.collect(Collectors.joining("-", "{", "}"));
System.out.println(result);
}
Output:
{1-2-3}
The Collectors.joining() method requires a CharSequence, so we need to map the Integer to String. We can utilize this same idea with other classes, even when we donβt have access to the code of the class.
5. Using an External Library
Now weβll use Apache Commonsβ StringUtils class to achieve similar results.
5.1. Maven Dependency
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
The latest version of the dependency can be found here.
5.2. Implementation
The implementation is literally a single method call:
@Test
public void whenStringUtilsJoin_thenPrintCustom() {
List<Integer> intList = Arrays.asList(1, 2, 3);
System.out.println(StringUtils.join(intList, "|"));
}
Output:
1|2|3
Again, this implementation is internally dependent on the toString() implementation of the type weβre considering.
6. Conclusion
In this article, we learned how easy it is to convert a List to a String using different techniques.
