RFR(s): 8060192: Add default method Collection.toArray(generator)
david.lloyd at redhat.com
Tue Dec 5 13:59:33 UTC 2017
On Tue, Dec 5, 2017 at 1:03 AM, Remi Forax <forax at univ-mlv.fr> wrote:
> 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);
> 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.
The signature of the proposed generator is
> public <T> T toArray(IntFunction<T> generator)
So I don't think that anything has really changed in this regard; T
can still be unrelated to E.
That said, sending in a constant String is probably just as good as
a generator, or better (in my naive estimation the tradeoff is a >0
comparison versus an interface method dispatch), than an i -> new
String[i] unless some kind of benchmark would appear which disproves
More information about the core-libs-dev