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

Remi Forax forax at univ-mlv.fr
Tue Dec 5 07:03:47 UTC 2017


Hi Martin,

----- Mail original -----
> De: "Martin Buchholz" <martinrb at google.com>
> À: "Stuart Marks" <stuart.marks at oracle.com>
> Cc: "core-libs-dev" <core-libs-dev at openjdk.java.net>
> Envoyé: Mardi 5 Décembre 2017 05:26:02
> Objet: Re: RFR(s): 8060192: Add default method Collection.toArray(generator)

> The needToWorkAround6260652 changes ought to be in a separate changeset.
> 
> The biggest question is whether Collection.toArray(generator) pulls its
> weight, especially in view of
> https://shipilev.net/blog/2016/arrays-wisdom-ancients.
> 
> I rarely want to dump elements into a typed array.  Dumping into Object[]
> with toArray() is just fine for me (but I'm a biased core library
> developer).

Dumping an ArrayList<String> into an array of String is fairly frequent, i think.

The main issue with the current API, <T> T[] toArray(T[] array), is that T can be unrelated to E (the type of the element in the collection) so one can write
  ArrayList<Integer> list = ...
  String[] array = list.toArray(new String[0]); 
it may even works if the list is empty.

It's getting worst if E and T can be a primitive type (with valhalla), because toArray(T[]) as to support all combinations.

So in my opinion, introducing toArray(generator) is a step in the right direction.

cheers,
Rémi


More information about the core-libs-dev mailing list