[PATCH] Use StringJoiner where appropriate in java.base

Kasper Nielsen kasperni at gmail.com
Thu Jun 20 13:29:59 UTC 2019


On Thu, 20 Jun 2019 at 13:53, Сергей Цыпанов <sergei.tsypanov at yandex.ru> wrote:
>
> On JDK 11 I've got the following results
>
> https://github.com/stsypanov/strings/blob/master/results/StringBuilderVsStringJoinerBenchmark.txt
>
> In other words StringJoiner wins when we have longer Strings or more of them because we don't need to reallocate underlying array of StringBuilder and check it's bounds when appending. Instead we can allocate storage only once in StringJoine::toString.

Hi,

Yes, StringJoiner is really good at joining Strings (hence the name).
But it is not very good at joining primitives. Because you have to
convert each primitive to a String before you can add it.
StringBuilder does not need to allocate a String when you add a
primitive.

Try running the same test but this time with int[] instead of
String[]. And I'm 100% sure that StringBuilder will be fastest.

/Kasper


More information about the core-libs-dev mailing list