I recently encountered with a Java 8 class StringJoiner which adds the String using the delimiters and adds prefix and suffix to it, but I can\'t understand the need of this
StringJoiner
is a kind of a Collector, although it doesn't implement the Collector
interface. It just behaves as such. Besides you can pass delimiter, prefix and suffix, the StringJoiner
may be employed to create formatted output from a Stream
invoking Collectors.joining(CharSequence)
.
This is especially useful when working with parallel streams, because at some point the batches that are being process in parallel will need to be joined and this is where the StringJoiner
takes place.