JavaTechnology

Convert List to Concatenated String with Delimiter in Java

A quick guide to Learn Various ways of converting a List of Strings into a concatenated String Joined by a delimiter in Java. We will see different examples to join the String.

Convert Manually, With and Without Delimiter

Firstly, concatenate a string without any delimiter.

List<String> words = Arrays.asList("Convert", "List", "of", "Strings", "To", "String");

String concatenatedString = "";
for(String word: words){
    concatenatedString += word;
}
System.out.println(concatenatedString);

As expected, the output is

ConvertListofStringsToString

Now, lets add a Delimiter. Remember, the delimiter should be appended at beginning of each word except the first one. We will add a space as a delimiter. However, you can use any character or a word as delimiter.

String concatenatedString = "";
String delimiter = " ";
for (String word : words) {
    concatenatedString += concatenatedString.equals("") ? word : delimiter + word;
}
System.out.println(concatenatedString);

And the output:

Convert List of Strings To String

However, you should not use this approach with the Strings. Because, Strings are immutable. Hence, every time you concat a String a separate String object is created. Consider using StringBuilder instead.

Using Java 8 String.join

Since Java 8, the String has new method – called as join.

List<String> words = Arrays.asList("Convert", "List", "of", "Strings", "To", "String");
String output = String.join(",", words);
System.out.println(output);
        
// Output:
// Convert,List,of,Strings,To,String

Moreover, this this approach also works with Set or any other collection that implements Iterable.

Set<String> set = Stream.of("Convert", "List", "of", "Strings", "To", "String").collect(Collectors.toSet());
String output = String.join(" ", set);
System.out.println(output);

// Output:
// Convert of List To String Strings

However, don’t forget that Set is an Unordered collection. Hence, you may see the sequence of words in output varies.

Using Java Streams to Concatenate String

Java Streams also provide a convenient way of Joining Streams. The streams api provides a special collector called as Collectors.joining specifically for joining Strings.

String output = Stream.of("Convert", "List", "of", "Strings", "To", "String")
                .collect(Collectors.joining(","));
System.out.println(output);

// Output:
// Convert,List,of,Strings,To,String

Moreover, you can also add a prefix and suffix to the concatenated string.

String output = Stream.of("Convert", "List", "of", "Strings", "To", "String")
                .collect(Collectors.joining(",", "[", "]"));
System.out.println(output);

// Output:
// [Convert,List,of,Strings,To,String]

Summary

In this short tutorial, we have seen different techniques of Joining a String from List of Strings in Java. We have seen a manual way of concatenating string with and without a delimiter. Also we saw the join method from the String class. Finally, we saw Java streams Collectors.joining method to join string using a delimiter along with a prefix and a suffix.


Leave a Reply

Your email address will not be published. Required fields are marked *