Stream.toArray() (original) (raw)

Remi Forax forax at univ-mlv.fr
Wed Dec 5 07:33:43 PST 2012


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[] toArray(Class 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



More information about the lambda-libs-spec-experts mailing list