为什么`Stream <t> :: <a>toArray(IntFunction<a[]>)` take a type parameter A with no bound?

时间:2019-01-23 01:00:31

标签: java arrays generics types java-stream

Because Stream<T>::<A>toArray(IntFunction<A[]>) and Collection<E>::<T>toArray(T[]) both take an unbounded type parameter, I can do this:

class Dog {}
class Cat {}

List<Dog> dogs = ...;

Cat[] cats1 = dogs.stream().toArray(Cat[]::new); // allowed
Cat[] cats2 = dogs.toArray(new Cat[0]); // also allowed

Why can't the method signature be changed to Stream<T>::<A super T>toArray(IntFunction<A[]>) and Collection<E>::<T super E>toArray(T[]) respectively?

0 个答案:

没有答案