在静态泛型方法中,<? extends E> 的用途是什么?

huangapple 未分类评论49阅读模式
英文:

In static generic method , what is the usage of <? extends E>?

问题

我正在阅读Java集合的源代码。我在java.util.List接口的源代码中遇到了一些问题。

在java.util.List接口的源代码底部,有一个方法:

  1. static <E> List<E> copyOf(Collection<? extends E> coll) {
  2. return ImmutableCollections.listCopy(coll);
  3. }

我想问一下,< ? extends E >的用法是什么?

我能否将< ? extends E >替换为E?就像这样:

  1. static <E> List<E> copyOf(Collection<E> coll) {
  2. return ImmutableCollections.listCopy(coll);
  3. }

这两个函数做的事情一样吗?

如果它们是相同的,为什么要使用< ? extends E >

如果它们不相同,当我调用方法时:

  1. List.copyOf(new ArrayList<Integer>());

其中的?(通配符)和E分别代表什么?

E是否表示Integer

还是?(通配符)表示Integer

英文:

I am reading source code of java collection. I have some troubles with the source code of Interface java.util.List.

In the bottom of source code of Interface java.util.List, there is a method:

  1. static &lt;E&gt; List&lt;E&gt; copyOf(Collection&lt;? extends E&gt; coll) {
  2. return ImmutableCollections.listCopy(coll);
  3. }

I want to ask, what is usage of &lt;? extends E&gt;.

Can I replace &lt;? extends E&gt; with E? Just like this:

  1. static &lt;E&gt; List&lt;E&gt; copyOf(Collection&lt;E&gt; coll) {
  2. return ImmutableCollections.listCopy(coll);
  3. }

Are two functions doing same things?

If they are same, why use &lt;? extends E&gt;

If they are not same,when I call the method like:

  1. List.copyOf(new ArrayList&lt;Integer&gt;());

what is ?(wildcard), and what is E?

Is E representing Integer?

or ?(wildcard) representing Integer?

huangapple
  • 本文由 发表于 2020年5月19日 17:18:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/61887495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定