RFR(s): 8060192: Add default method Collection.toArray(generator)

Stuart Marks stuart.marks at oracle.com
Mon Jun 18 23:26:44 UTC 2018


Hi Rémi,

On 6/15/18 12:26 AM, Remi Forax wrote:
>> The overrides I had previously provided in specific implementation classes like
>> ArrayList actually are slower, because the allocation of the array is done
>> separately from filling it. This necessitates the extra zero-filling step. Thus,
>> I've removed the overrides.
> 
> for ArrayList, you may use a code like this,
>    <T> T[] toArray(IntFunction<T[]> generator) {
>      return (T[]) Arrays.copyOf(elementData, size, generator.apply(0).getClass());
>    }
> so you win only one comparison (yeah !), which can be perfectly predicted, so you should not see any perf difference :)

True, this will probably work better than the previous code (allocate correct 
size, fill with System.arraycopy) but it doesn't seem likely to be any faster 
than the default method.

> List<String>.class or List<String>[].class do not work either.

I think they can be made to work (see other sub-thread with Peter Levart) but I 
don't see any advantages going in that direction.

>> For these reasons I'd like to proceed with adding toArray(generator) API.
> 
> so thumb up for me !

Great!

s'marks



More information about the core-libs-dev mailing list