Convert a List to a String with Separator in Java
This post will discuss how to convert a List to a String in Java. The solution should concatenate all items in the List to a String using a separator.
1. Using String.join() method
Since Java 8, you can use the String.join() method to join strings together using the specified separator. It only works on a List of type String. Here’s how the code will look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> words = Arrays.asList("A", "B", "C", "D"); String sep = ", "; String str = String.join(sep, words); System.out.println(str); } } |
Output:
A, B, C, D
2. Using Stream API
Java 8 Stream API provides the joining collector that can concatenate the elements of a stream, separated by the specified delimiter. Its usage is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<Integer> input = Arrays.asList(1, 2, 3, 4); String sep = ", "; String str = input.stream().map(Object::toString) .collect(Collectors.joining(sep)); System.out.println(str); } } |
Output:
1, 2, 3, 4
3. Using Apache Commons Lang
Apache Commons Lang StringUtils class provides the join() method which can concatenate list elements together with a specified separator, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import org.apache.commons.lang3.StringUtils; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> input = Arrays.asList(1, 2, 3, 4); String sep = ", "; String str = StringUtils.join(input, sep); System.out.println(str); } } |
Output:
1, 2, 3, 4
4. Using Guava
If you prefer Guava to Apache Commons Lang, you might want to check out the flexible Joiner class. The following code demonstrates its usage by creating a Joiner, configuring the separator, and specifying the collection to be joined.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.base.Joiner; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> input = Arrays.asList(1, 2, 3, 4); String sep = ", "; String str = Joiner.on(sep).join(input); System.out.println(str); } } |
Output:
1, 2, 3, 4
5. Using toString() method
If you just need to print the string representation of the list, you can do so by calling the toString() on your list. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> input = Arrays.asList(1, 2, 3, 4); String str = input.toString(); System.out.println(str); } } |
Output:
[1, 2, 3, 4]
That’s all about converting a List to a String in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)