In this tutorial we will see the difference between StringJoiner and String.join(). These were introduced into Java 8 version. The example we create here will show how to join multiple string objects or literals or list of strings using append(), StringJoiner, String.join() API.

Situation may occur when you need to join multiple string literals or objects into one. You may also find a situation when you need to convert a list of strings or a collection of strings into csv(comma separated value) strings.

Until JDK 8 for joining such multiple strings we had to rely on manual process using loop to join multiple string objects. In this approach we had to be extra careful while appending delimiter before first and after last elements because we don’t want to append delimiter before first and after last elements. This kind of situation becomes complex for new programmers or beginners.

This requirement became routines for most of the applications and many programmers had to write the code for joining multiple strings separated by delimiter using loop and append() method. The append() method is available only in StringBuffer and StringBuilder classes. But Java 8 solves the problem once for all.

It is not that you cannot use the loop to join multiple strings but Java 8 provides easy to use solution and you can use either StringJoiner class or String.join() method to accomplish the same. The advantage of using built-in API is, you don’t need to be extra cautious while joining multiple string literals or objects.

Join using append()

We will see how to join multiple strings using append() method.

The first example using append() method joins string delimited by /. The separator or delimiter is not added at the either end of the joined string.

String sep = "";
StringBuilder sb = new StringBuilder();

for (int i = 0; i < 5; i++) {
	String uuid = UUID.randomUUID().toString();
	sep = "/";


The above code snippets joins the random UUID and produces the below output.


If you want to add delimiter or separator at either ends then you need to remove empty string from the separator initialization. Full source code available later.

Join using StringJoiner

Now JDK 8 provides a ready made solution using StringJoiner class and with just one line you can append or join multiple strings.

The following example produces the path separated by /.

StringJoiner stringJoiner = new StringJoiner("/");



The output is etc/var/cache

If you need to add / at both ends then you can use the following example:

StringJoiner stringJoiner = new StringJoiner("/", "/", "/");


The above code produces output /etc/var/cache/

Join using String.join()

We can also join multiple strings using String.join() method.

String joinedString = String.join(sep, "etc", "var", "cache");


The above code snippets will produce output etc/var/cache

So you don’t need to be careful about not adding delimiter at the start or end of the string.

Using String.join() method you can directly convert list of strings into a delimited string.

String sep = "/";

List<String> strings = new ArrayList<>();

for (int i = 0; i < 5; i++) {
	String uuid = UUID.randomUUID().toString();

String joinedString = String.join(sep, strings);


The above will produce a joined string separated by / from the list of strings.

Source Code


Thanks for reading.


Leave a Reply

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