Stream.toArray()
David M. Lloyd
david.lloyd at redhat.com
Wed Dec 5 07:44:39 PST 2012
Agreed on the array class - another reason is that otherwise folks might
expect this to work:
int[] foo = stream.toArray(int[].class);
On 12/05/2012 09:39 AM, Brian Goetz wrote:
> Agree on the general form -- toArray(clazz) is definitely better than
> the current bad alternatives offered by Collection.
>
> I prefer that the argument be the component class, not the array class.
> I think toArray(Foo.class) is far more natural to users than
> toArray(Foo[].class).
>
> On 12/5/2012 10:33 AM, Remi Forax wrote:
>> Restarting a thread that ends without clear winner.
>>
>> Currently, Stream.toArray() is specified as:
>> Object[] toArray()
>>
>> which is not what users want, given the lack of reified generics and the
>> fact that it's usually hard for a user to predict the number of elements
>> of a Stream,
>> the best signature seems to be:
>> <A> A[] toArray(Class<A> arrayClass)
>> with arrayClass.isArray() returning true and
>> arrayClass.getComponentType().isPrimitive() returning false
>> (or if you prefer Object[].class.isAssignableFrom(arrayClass) returning
>> true)
>>
>> example of usage,
>> Person[] coolPersons =
>> persons.stream().filter(person#isCool()).toArray(Person[].class);
>>
>> cheers,
>> Rémi
>>
--
- DML
More information about the lambda-libs-spec-observers
mailing list