Given a Java Collection, construct a new String containing string representation of its elements separated by the specified delimiter.
1. Using StringBuilder
The idea here is to iterate through the collection and append each element to a StringBuilder along with the specified delimiter. Finally, return 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 24 25 26 27 28 29 30 31 32 33 |
import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.List; class CollectionUtils { // Merge elements of the given Collection into a String // separated by the specified delimiter public static<T> String mergeCollection(Collection<T> collection, String delim) { if (collection == null || collection.isEmpty()) return ""; Iterator<T> it = collection.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) { Collection<String> strings = Arrays.asList("I", "love", "Java"); String message = mergeCollection(strings, "-"); System.out.println(message); } } |
Output:
I-love-Java
2. Java 8 – StringJoiner Class
In previous approach, we’re appending the delimiter to StringBuilder for every pair of consecutive elements in the collection. From Java 8 onward, we can use StringJoiner class which takes the separator during initialization itself.
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.Collection; import java.util.List; import java.util.StringJoiner; class CollectionUtils { // Merge elements of the given Collection into a String // separated by the specified delimiter public static<T> String mergeCollection(Collection<T> collection, String delim) { if (collection == null) return ""; StringJoiner sj = new StringJoiner(delim); for (T element: collection) { sj.add(element.toString()); } return sj.toString(); } public static void main(String[] args) { Collection<String> strings = Arrays.asList("I", "love", "Java"); String message = mergeCollection(strings, "-"); System.out.println(message); } } |
Output:
I-love-Java
3. Guava Joiner
Similar to StringJoiner class, Guava library Joiner class is designed to solve the similar problem. The idea remains the same – create a joiner, and configure the separator and specify the collection to be added.
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 com.google.common.base.Joiner; import java.util.Arrays; import java.util.Collection; import java.util.List; class CollectionUtils { // Merge elements of the given Collection into a String // separated by the specified delimiter public static<T> String mergeCollection(Collection<T> collection, String delim) { if (collection == null) return ""; return Joiner.on(delim) .join(collection); } public static void main(String[] args) { Collection<String> strings = Arrays.asList("I", "love", "Java"); String message = mergeCollection(strings, "-"); System.out.println(message); } } |
Output:
I-love-Java
4. Java 8 – String.join()
StringJoiner class provides the flexibility of adding the selected elements from the collection. In case we need to add the whole Collection, we can use static join() method of the String class from Java 8 onward.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Arrays; import java.util.Collection; import java.util.List; class CollectionUtils { // Merge elements of the given Collection into a String // separated by the specified delimiter public static String mergeCollection(Collection<String> collection, String delim) { if (collection == null) return ""; return String.join(delim, collection); } public static void main(String[] args) { Collection<String> strings = Arrays.asList("I", "love", "Java"); String message = mergeCollection(strings, "-"); System.out.println(message); } } |
Output:
I-love-Java
Thanks for reading.
Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
Leave a Reply