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();
	sb.append(sep);
	sb.append(uuid);
	sep = "/";
}

System.out.println(sb.toString());

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

2ba3554d-f1e4-49e2-80e0-dc537e933ee4/fa7dbf0e-d6b7-43b8-b865-a0d52f545e32/8780b713-2762-41cd-818c-74f95d1501c6/ecdfa22f-25e2-405d-80a5-3096686a1fec/ded81b6c-663d-4dbc-b582-7068411397f6

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("/");

stringJoiner.add("etc");
stringJoiner.add("var");
stringJoiner.add("cache");

System.out.println(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("/", "/", "/");
stringJoiner.add("etc");
stringJoiner.add("var");
stringJoiner.add("cache");

System.out.println(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");

System.out.println(joinedString);

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();
	strings.add(uuid);
}

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

System.out.println(joinedString);

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

Source Code

Download

Thanks for reading.

Tags:

Leave a Reply

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