Java Reference ยท Text, Formatting and Regex
The StringJoiner class
Reference for java.util.StringJoiner: join pieces with a delimiter, prefix and suffix, set an empty value, merge joiners, and compare with String.join and Collectors.
java.util.StringJoiner builds a string from pieces separated by a delimiter, with an optional prefix at the start and suffix at the end. It removes the classic "is this the last element?" logic from loops: you simply add pieces, and the delimiter appears only between them.
Most of the time you do not need to create one yourself, because String.join() and Collectors.joining() use it internally. Reach for StringJoiner directly when pieces arrive one by one from code that is not a simple collection, or when you need setEmptyValue or merge.
StringJoiner sj = new StringJoiner(", ", "[", "]"); sj.add("a").add("b"); System.out.println(sj); // [a, b]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public final class StringJoiner public StringJoiner(CharSequence delimiter) public StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
| Member | Description |
|---|---|
StringJoiner add(CharSequence) |
Appends a piece; returns the joiner for chaining. A null argument adds the text "null". |
StringJoiner setEmptyValue(CharSequence) |
Text returned by toString() while no piece has been added. |
StringJoiner merge(StringJoiner other) |
Adds the contents of other (without its prefix and suffix) as a single piece. |
int length() |
Length of the string toString() would return. |
String toString() |
The joined text. |
Throws: NullPointerException if the delimiter, prefix, suffix or the empty value is null, or if merge receives null.
Usage notes
- Since Java 8. It is a final class; it does not implement
CharSequence. - Empty case. With no pieces,
toString()is the prefix plus the suffix, unless you calledsetEmptyValue. This matters for output such as[]versus(none). add("")counts as a piece, so the delimiter appears around empty strings. Skip empty values yourself if you do not want them.- Mutable, not thread-safe. Do not share one joiner between threads without locking.
mergeis not concatenation. The other joiner's own delimiter, prefix and suffix are dropped; its content becomes one element joined with this joiner's delimiter.- Prefer simpler tools when possible:
String.join(delim, iterable)for existing collections andCollectors.joiningfor streams. For general text building,StringBuilderis more flexible. - Performance is linear in the total length.