Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use String.join to turn values you already have into a string with a delimiter. Use StringJoiner when you want to add values incrementally or wrap the result with a prefix and suffix. For values in a stream pipeline, use Collectors.joining.

Choose the right Java 8 join API

Situation Use Why
Values are already available as individual arguments or an iterable String.join It directly returns a String with the delimiter between values.
Values arrive one at a time, or the output needs a prefix and suffix StringJoiner It supports incremental additions and configurable wrappers.
Values are flowing through a stream pipeline Collectors.joining It collects stream elements into a string in encounter order.

These APIs accept CharSequence values, such as String; they do not apply custom formatting to arbitrary objects. Convert or map other values to text before joining. See Oracle’s Java 8 CharSequence API.

Join values with String.join

Java 8 adds two String.join overloads: one accepts a delimiter and varargs, and the other accepts a delimiter and an Iterable<? extends CharSequence>. Both return a new string with the delimiter placed between elements. The methods are marked “Since: 1.8” in Oracle’s Java SE 8 String API.

Join individual values

String message = String.join("-", "Java", "is", "cool");
// Java-is-cool

The first argument is the delimiter; the remaining arguments are the values to join.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Join a collection

List<String> words = Arrays.asList("Java", "is", "cool");
String message = String.join(" ", words);
// Java is cool

The collection overload accepts an iterable of character sequences. The result follows the iterable’s iteration order, so use an ordered collection when output order matters.

Build a result incrementally with StringJoiner

Create a StringJoiner with a delimiter, then call add for each value. To surround the joined content, use the constructor that also takes a prefix and suffix. Calling toString() produces the result.

StringJoiner joiner = new StringJoiner(", ", "[", "]");
joiner.add("red").add("green").add("blue");
String result = joiner.toString();
// [red, green, blue]

add returns the same joiner, which allows calls to be chained. Oracle documents the constructors, additions, and string representation in its Java SE 8 StringJoiner API.

Control the empty result

An empty joiner with no wrappers produces an empty string. An empty joiner constructed with a prefix and suffix produces those two strings together. Use setEmptyValue to choose a different representation while the joiner has no elements:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
StringJoiner joiner = new StringJoiner(", ", "[", "]");
joiner.setEmptyValue("[]");
String result = joiner.toString();
// []

Once add has been called, the joiner is no longer considered empty—even if the added value is an empty string—so the empty-value replacement no longer applies.

Merge joiners when combining accumulated results

merge appends the other joiner’s contents without its prefix or suffix. If the joiners use different delimiters, the other joiner’s accumulated text is appended as a single element, so its internal delimiter remains part of that text.

Join values from a stream

For a stream pipeline, collect with Collectors.joining. Its overloads concatenate stream values in encounter order, with optional delimiter, prefix, and suffix, as documented in Oracle’s Java SE 8 Collectors API.

String result = words.stream()
    .collect(Collectors.joining(", "));

To wrap the joined output, provide all three strings:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
String result = words.stream()
    .collect(Collectors.joining(", ", "[", "]"));
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Nulls and other edge cases

  • Null elements: String.join and StringJoiner.add render a null element as the text null; they do not skip it.
  • Null delimiter: String.join and the StringJoiner constructors reject a null delimiter with NullPointerException. The three-argument StringJoiner constructor also rejects a null prefix or suffix.
  • Null input container: String.join rejects a null varargs array or a null iterable.
  • Empty input: Joining no values adds no element text. For StringJoiner, the rendered result depends on its wrappers and any configured empty value.

Oracle’s Java 8 String documentation states: “Note that if an element is null, then “null” is added.”

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.