Why StringJoiner when we already have StringBuilder?

后端 未结 5 1693
渐次进展
渐次进展 2020-12-01 04:30

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

5条回答
  •  不思量自难忘°
    2020-12-01 04:36

    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.

提交回复
热议问题