Join elements of a list by a delimiter in Java
This post will discuss how to join elements of a list in Java separated by a delimiter.
1. Using Collectors.joining() method
A simple and efficient way to join elements of a list by a delimiter in Java is using Java 8 features such as Stream API. We can use the stream() method to convert the list into a stream of strings, then use the map() method to apply any transformation to each element (such as calling toString()), and then use the collect() method with the Collectors.joining() collector to join all the elements by a delimiter. For example:
|
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, 4); String delim = "-"; String joined = list.stream() .map(Object::toString) .collect(Collectors.joining(delim)); System.out.println(joined); // 1-2-3-4 } } |
2. Using String.join() method
The String.join() is a static method introduced in Java 8 that takes a delimiter and an iterable of strings (such as a list) as arguments and returns a single string with all the elements joined by the delimiter. For example:
|
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> list = Arrays.asList("I", "love", "Java"); String delim = "-"; String joined = String.join(delim, list); System.out.println(joined); // I-love-Java } } |
3. Using StringBuilder Class
Another way to join elements of a list is using the StringBuilder class. The idea is to create an instance of the StringBuilder class and append each element of the list to it, along with the delimiter. We can use a loop to iterate over the list and check if the current element is the last one or not. If it is not the last one, we can append the delimiter after it. Otherwise, we can skip the delimiter. Finally, we return the string representation of the StringBuilder. For example, to join a list of strings with a hyphen as a delimiter, we can write:
|
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 26 27 28 29 30 31 |
import java.util.Arrays; import java.util.Iterator; import java.util.List; class Main { public static<T> String joinCollection(List<T> list, String delim) { if (list == null || list.isEmpty()) { return ""; } Iterator<T> it = list.iterator(); StringBuilder sb = new StringBuilder(it.next().toString()); while (it.hasNext()) { sb.append(delim); sb.append(it.next()); } return sb.toString(); } public static void main(String[] args) { List<String> strings = Arrays.asList("I", "love", "Java"); String joined = joinCollection(strings, "-"); System.out.println(joined); // I-love-Java } } |
4. Using StringJoiner Class
In the previous approach, we’re appending the delimiter to StringBuilder for every pair of consecutive elements in the list. From Java 8 onward, we can use the StringJoiner class, that allows us to create a string with a specified delimiter, prefix, and suffix. We can use the add() method to append each element of the list to the string joiner, and then use the toString() method to get the final string. For example:
|
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 26 27 |
import java.util.Arrays; import java.util.List; import java.util.StringJoiner; class Main { public static<T> String joinCollection(List<T> list, String delim) { if (list == null) { return ""; } StringJoiner sj = new StringJoiner(delim); for (T element: list) { sj.add(element.toString()); } return sj.toString(); } public static void main(String[] args) { List<String> strings = Arrays.asList("I", "love", "Java"); String joined = joinCollection(strings, "-"); System.out.println(joined); // I-love-Java } } |
5. Using Guava’s Joiner Class
Similar to the StringJoiner class, Guava library Joiner class is designed to tackle a similar problem. The basic usage of the Joiner class is to create an instance with a specified delimiter using the on() method, and then call the join() method with the list of elements to be joined. For example:
|
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<Integer> list = Arrays.asList(1, 2, 3, 4); String delim = "-"; String joined = Joiner.on(delim).join(list); System.out.println(joined); // 1-2-3-4 } } |
The Joiner class also provides some useful methods to handle null values, such as skipNulls() and useForNull(). The skipNulls() method returns a new Joiner instance that skips over any null elements in the input. The useForNull() method returns a new Joiner instance that replaces any null elements with a given string. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import com.google.common.base.Joiner; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> list = Arrays.asList("I", "love", null); String delim = "-"; String joined = Joiner.on(delim).skipNulls().join(list); System.out.println(joined); // I-love joined = Joiner.on(delim).useForNull("Unknown").join(list); System.out.println(joined); // I-love-Unknown } } |
6. Using Apache Commons Lang
Apache Commons Lang is another external library that provide methods to join elements of a list by a delimiter. For example, we can use the StringUtils.join() method from Apache Commons Lang like this:
|
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<Integer> list = Arrays.asList(1, 2, 3, 4); String delim = "-"; String joined = StringUtils.join(list, delim); System.out.println(joined); // 1-2-3-4 } } |
That’s all about joining elements of a list in Java separated by a delimiter.
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 :)