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

Paul Sandoz paul.sandoz at oracle.com
Tue Dec 5 18:47:19 UTC 2017

> On 4 Dec 2017, at 19:20, Stuart Marks <stuart.marks at oracle.com> wrote:
> Hi all,
> Please review this small enhancement to add a default method Collection.toArray(generator) that takes a function that creates the destination array. This is analogous to Stream.toArray(generator).
> Bug:
>    https://bugs.openjdk.java.net/browse/JDK-8060192

 345      * <p>Suppose {@code x} is a collection known to contain only strings.
 346      * The following code can be used to dump the collection into a newly
 347      * allocated array of {@code String}:

Make it an API note? (same for toArray(T[])

 352      * @implSpec
 353      * The default implementation calls the generator function with zero
 354      * and then passes the resulting array to {@link #toArray(T[])}.

That’s reasonable. I pondered about being vague and specifying a value in the range if [0, size()), to allow wiggle room for using 0 or size().

 360      * @throws ArrayStoreException if the runtime type of any element in this
 361      *         collection is not assignable to the {@linkplain Class#getComponentType
 362      *         runtime component type} of array returned by the generator function

s/of array returned by the generator function/of the generated array/ ?


> Webrev:
>    http://cr.openjdk.java.net/~smarks/reviews/8060192/webrev.0/
> Thanks,
> s'marks

More information about the core-libs-dev mailing list