RFR: 8305774: String.join(CharSequence, Iterable) can be optimized if Iterable is a Collection [v2]

Tingjun Yuan duke at openjdk.org
Mon Apr 10 03:18:48 UTC 2023


On Mon, 10 Apr 2023 03:04:20 GMT, Chen Liang <liach at openjdk.org> wrote:

>> @liach Working on that.
>
> @yuantj Alternatively, you can probably try working on the `toArray` result of a collection than to allocate a new String array. This might be more efficient for some implementations like ArrayList as well, in addition to benefiting concurrent collections.

@liach  Did you mean this?


public static String join(CharSequence delimiter,
            Iterable<? extends CharSequence> elements) {
    if (elements instanceof Collection<?> c) {
        try {
            return String.join(delimiter, c.toArray(new CharSequence[0]));
        } catch (ArrayStoreException e) {
            throw new ClassCastException(e.getMessage());
        }
    } else {
        // old implementations for non-collection iterables.
    }
}

-------------

PR Comment: https://git.openjdk.org/jdk/pull/13383#issuecomment-1501340839


More information about the core-libs-dev mailing list