Showcase of StringJoiner API.
Please refer my other github project about joining Strings (using Collectors.joining or String.join): Collectors.joining.
- constructors
public StringJoiner(CharSequence delimiter) public StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) - methods
public StringJoiner setEmptyValue(CharSequence emptyValue)- Sets the sequence of characters to be used when determining the string representation of thisStringJoinerand no elements have been added yet, that is, when it is empty. Note that once an add method has been called, theStringJoineris no longer considered empty, even if the element(s) added correspond to the emptyString.public StringJoiner add(CharSequence newElement)public StringJoiner merge(StringJoiner other)- Merge adds the contents of the givenStringJoinerwithout prefix and suffix as the next element if it is non-empty. If the givenStringJoineris empty, the call has no effect.
We provide tests for mentioned above methods:
- "a", "b", "c" -> "a,b,c"
var stringJoiner = new StringJoiner(","); List.of("a", "b", "c").forEach(stringJoiner::add); - "a", "b", "c" -> "prefix-a,b,c-suffix"
var stringJoiner = new StringJoiner(",", "prefix-", "-suffix"); List.of("a", "b", "c").forEach(stringJoiner::add); - merge two different
StringJoinersvar stringJoinerComma = new StringJoiner(","); List.of("a", "b", "c").forEach(stringJoinerComma::add); var stringJoinerDot = new StringJoiner("."); List.of("d", "e", "f").forEach(stringJoinerDot::add); var merged = stringJoinerComma.merge(stringJoinerDot); assertThat(merged.toString(), is("a,b,c,d.e.f")); - empty
StringJoinerwith / without prefix and suffixvar stringJoiner = new StringJoiner(","); assertThat(stringJoiner.toString(), is(""));var stringJoiner = new StringJoiner(",", "prefix-", "suffix"); assertThat(stringJoiner.toString(), is("prefix-suffix")); - configure empty
StringJoiner-setEmptyValuesvar stringJoiner = new StringJoiner(","); stringJoiner.setEmptyValue("empty"); assertThat(stringJoiner.toString(), is("empty"));var stringJoiner = new StringJoiner(",", "prefix-", "-suffix"); stringJoiner.setEmptyValue("empty"); assertThat(stringJoiner.toString(), is("empty"));
Collectors.joining(...) and String.join(...) are using internally
StringJoiner (or StringBuilder in the easiest case):
- Collectors
public static Collector<CharSequence, ?, String> joining() { return new CollectorImpl<CharSequence, StringBuilder, String>( StringBuilder::new, StringBuilder::append, (r1, r2) -> { r1.append(r2); return r1; }, StringBuilder::toString, CH_NOID); }public static Collector<CharSequence, ?, String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) { return new CollectorImpl<>( () -> new StringJoiner(delimiter, prefix, suffix), StringJoiner::add, StringJoiner::merge, StringJoiner::toString, CH_NOID); } - String
public static String join(CharSequence delimiter, CharSequence... elements) { Objects.requireNonNull(delimiter); Objects.requireNonNull(elements); // Number of elements not likely worth Arrays.stream overhead. StringJoiner joiner = new StringJoiner(delimiter); for (CharSequence cs: elements) { joiner.add(cs); } return joiner.toString(); }