英文:
In static generic method , what is the usage of <? extends E>?
问题
我正在阅读Java集合的源代码。我在java.util.List接口的源代码中遇到了一些问题。
在java.util.List接口的源代码底部,有一个方法:
static <E> List<E> copyOf(Collection<? extends E> coll) {
return ImmutableCollections.listCopy(coll);
}
我想问一下,< ? extends E >
的用法是什么?
我能否将< ? extends E >
替换为E
?就像这样:
static <E> List<E> copyOf(Collection<E> coll) {
return ImmutableCollections.listCopy(coll);
}
这两个函数做的事情一样吗?
如果它们是相同的,为什么要使用< ? extends E >
?
如果它们不相同,当我调用方法时:
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:
static <E> List<E> copyOf(Collection<? extends E> coll) {
return ImmutableCollections.listCopy(coll);
}
I want to ask, what is usage of <? extends E>.
Can I replace <? extends E> with E? Just like this:
static <E> List<E> copyOf(Collection<E> coll) {
return ImmutableCollections.listCopy(coll);
}
Are two functions doing same things?
If they are same, why use <? extends E>
If they are not same,when I call the method like:
List.copyOf(new ArrayList<Integer>());
what is ?(wildcard), and what is E?
Is E representing Integer?
or ?(wildcard) representing Integer?
专注分享java语言的经验与见解,让所有开发者获益!
评论