Convert a List to a Java String
This post will discuss how to convert a list to a string in Java. The solution should join elements of the provided list into a single string with a given delimiter. The solution should not add a delimiter before or after the list.
1. Using StringBuilder
The idea is to loop through the list and concatenate each list element to the StringBuilder instance with the specified delimiter. Finally, we return the string representation of the StringBuilder. Note that the solution should deal with trailing delimiters’ characters.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> list = Arrays.asList("A", "B", "C"); String delim = "-"; StringBuilder sb = new StringBuilder(); int i = 0; while (i < list.size() - 1) { sb.append(list.get(i)); sb.append(delim); i++; } sb.append(list.get(i)); String res = sb.toString(); System.out.println(res); // A-B-C } } |
2. Using Java 8 and above
The above solution is not recommended for Java 8 and above. From Java 8 onward, we can use the static join() method of the String class, which joins strings together with a specified delimiter.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> list = Arrays.asList("A", "B", "C"); String delim = "-"; String res = String.join(delim, list); System.out.println(res); // A-B-C } } |
Note that the above method works only on a list of strings. If our list is not of string type, a joining collector can be used, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3); String delim = "-"; String res = list.stream() .map(Object::toString) .collect(Collectors.joining(delim)); System.out.println(res); // 1-2-3 } } |
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.List; import java.util.StringJoiner; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3); String delim = "-"; StringJoiner joiner = new StringJoiner(delim); for (Integer integer : list) { joiner.add(integer.toString()); } String res = joiner.toString(); System.out.println(res); // 1-2-3 } } |
3. Using Guava’s Joiner Class
Like the String.join() method, Guava provides Joiner class, which can joins elements of a list using a delimiter.
|
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> list = Arrays.asList(1, 2, 3); String delim = "-"; String res = Joiner.on(delim).join(list); System.out.println(res); // 1-2-3 } } |
4. Using Apache Commons Lang
Finally, we can also use the Apache Commons Lang library for our purpose. StringUtils class offers the join() method, which takes the list and separator. It joins the elements of the provided list into a single string containing the provided list of elements.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import org.apache.commons.lang3.StringUtils; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3); String delim = "-"; String res = StringUtils.join(list, delim); System.out.println(res); // 1-2-3 } } |
That’s all about converting a List to a Java String.
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 :)