{@code StringJoiner} is used to construct a sequence of characters separatedby a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.
Prior to adding something to the {@code StringJoiner}, its {@code sj.toString()} method will, by default, return {@code prefix + suffix}. However, if the {@code setEmptyValue} method is called, the {@code emptyValue}supplied will be returned instead. This can be used, for example, when creating a string using set notation to indicate an empty set, i.e. "{}"
, where the {@code prefix} is "{"
, the{@code suffix} is "}"
and nothing has been added to the{@code StringJoiner}.
@apiNote
The String {@code "[George:Sally:Fred]"} may be constructed as follows:
{@code StringJoiner sj = new StringJoiner(":", "[", "]"); sj.add("George").add("Sally").add("Fred"); String desiredString = sj.toString();}
A {@code StringJoiner} may be employed to create formatted output from a{@link java.util.stream.Stream} using{@link java.util.stream.Collectors#joining(CharSequence)}. For example:
{@code List numbers = Arrays.asList(1, 2, 3, 4); String commaSeparatedNumbers = numbers.stream() .map(i -> i.toString()) .collect(Collectors.joining(", "));}
@see java.util.stream.Collectors#joining(CharSequence)
@see java.util.stream.Collectors#joining(CharSequence,CharSequence,CharSequence)
@since 1.8