Convert a List to Comma-Separated String in Java
This post will discuss how to convert a List to a comma-separated String in Java.
1. Using Apache Commons Lang
Apache Commons Lang library offers the StringUtils.join() method, which can concatenate elements of an iterable together using the specified separator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import org.apache.commons.lang3.StringUtils; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> words = Arrays.asList("Hello", "World"); String str = StringUtils.join(words, ','); System.out.println(str); } } |
Output:
Hello,World
2. Using Guava
Similar to Apache Commons Lang, the Guava library offers a Joiner class to join elements of an iterable using a separator between consecutive elements.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.base.Joiner; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> words = Arrays.asList("Hello", "World"); String str = Joiner.on(',').join(words); System.out.println(str); } } |
Output:
Hello,World
3. Using String.join() method
Since Java 8, you can use the String.join() method that works only for Iterable<String>. It joins strings together with a specified separator.
|
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<String> words = Arrays.asList("Hello", "World"); String str = String.join(",", words); System.out.println(str); } } |
Output:
Hello,World
4. Stream API
From Java 8 onwards, this can be efficiently done using joining Collector:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<String> words = Arrays.asList("Hello", "World"); String str = words.stream().collect(Collectors.joining(",")); System.out.println(str); } } |
Output:
Hello,World
5. Using StringBuilder
Finally, you can iterate through the list and append each value to a StringBuilder along with the specified delimiter. The following solution demonstrates this by creating a utility function that returns the string representation of the StringBuilder.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Arrays; import java.util.List; class Main { public static<T> String join(List<T> list) { StringBuilder sb = new StringBuilder(); String sep = ""; for (T e : list) { sb.append(sep).append(e); sep = ","; } return sb.toString(); } public static void main(String[] args) { List<String> words = Arrays.asList("Hello", "World"); String str = join(words); System.out.println(str); } } |
Output:
Hello,World
That’s all about converting a List to a comma-separated 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 :)