Before Java generics, Collection.toArray()
had no way to know which type of array the developer expected (particularly for an empty collection). As I understand it, this was the main rationale behind the idiom collection.toArray(new E[0])
.
With generics, Collection<E>.toArray()
can only return an array full of instances of E
and/or its specialisations. I wonder why the return type still is as Object[]
rather than E[]
. In my opinion, returning an E[]
instead of Object[]
should not break existing code.
See: Collection.toArray()
, Collection.toArray(T[])
and the related topic java: (String[])List.toArray() gives ClassCastException