为什么 addAll(Collection<? extends E>) 用于将元素放入集合中,而通配符 extends 通常建议用于 Get?

Why addAll(Collection<? extends E>) is used to put elements in a collection while wildcard extends is generally suggested for Get?

我明白了“? extends E”的意思是可以全部加起来 具有作为 E 的子类型的任何类型的元素的集合的成员。 因此对于 List , addAll(List 的所有值) 将保持良好状态。 但是,为什么根据 Get 和 Put 原则将 "Use an extends wildcard when you only get values out of a structure" 概括为?

当您从参数 CollectionaddAll 时,您 "getting" 将 Collection 参数中的所有元素添加到另一个集合中。因此"Producer Extends (Consumer Super)"在这里仍然适用。

通过强制执行通用 <? extends E>,我们确保参数中的所有元素都是 instanceof E,因此可以成功添加到 Collection<E>.