有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么'Stream<T>::<A>toArray(IntFunction<A[]>)`接受没有绑定的类型参数A?

因为Stream<T>::<A>toArray(IntFunction<A[]>)Collection<E>::<T>toArray(T[])都采用无界类型参数,所以我可以这样做:

class Dog {}
class Cat {}

List<Dog> dogs = ...;

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

为什么方法签名不能分别更改为Stream<T>::<A super T>toArray(IntFunction<A[]>)Collection<E>::<T super E>toArray(T[])


共 (0) 个答案