What is the difference between List<? …
2018-11-02 08:49:34来源:博客园 阅读 ()
What is the difference between List<? super T> and List<? extends T> ?
extends
The wildcard declaration of List<? extends Number> foo3 means that any of these are legal assignments:
List<? extends Number> foo3 = new ArrayList<Number>(); // Number "extends" Number (in this context)
List<? extends Number> foo3 = new ArrayList<Integer>(); // Integer extends Number
List<? extends Number> foo3 = new ArrayList<Double>(); // Double extends Number
-
Reading - Given the above possible assignments, what type of object are you guaranteed to read from
List foo3:- You can read a
Numberbecause any of the lists that could be assigned tofoo3contain aNumberor a subclass ofNumber. - You can't read an
Integerbecausefoo3could be pointing at aList<Double>. - You can't read a
Doublebecausefoo3could be pointing at aList<Integer>.
- You can read a
-
Writing - Given the above possible assignments, what type of object could you add to
List foo3that would be legal for all the above possibleArrayListassignments:- You can't add an
Integerbecausefoo3could be pointing at aList<Double>. - You can't add a
Doublebecausefoo3could be pointing at aList<Integer>. - You can't add a
Numberbecausefoo3could be pointing at aList<Integer>.
- You can't add an
You can't add any object to List<? extends T> because you can't guarantee what kind of List it is really pointing to, so you can't guarantee that the object is allowed in that List. The only "guarantee" is that you can only read from it and you'll get a T or subclass of T.
super
Now consider List <? super T>.
The wildcard declaration of List<? super Integer> foo3 means that any of these are legal assignments:
List<? super Integer> foo3 = new ArrayList<Integer>(); // Integer is a "superclass" of Integer (in this context)
List<? super Integer> foo3 = new ArrayList<Number>(); // Number is a superclass of Integer
List<? super Integer> foo3 = new ArrayList<Object>(); // Object is a superclass of Integer
-
Reading - Given the above possible assignments, what type of object are you guaranteed to receive when you read from
List foo3:- You aren't guaranteed an
Integerbecausefoo3could be pointing at aList<Number>orList<Object>. - You aren't guaranteed a
Numberbecausefoo3could be pointing at aList<Object>. - The only guarantee is that you will get an instance of an
Objector subclass ofObject(but you don't know what subclass).
- You aren't guaranteed an
-
Writing - Given the above possible assignments, what type of object could you add to
List foo3that would be legal for all the above possibleArrayListassignments:- You can add an
Integerbecause anIntegeris allowed in any of above lists. - You can add an instance of a subclass of
Integerbecause an instance of a subclass ofIntegeris allowed in any of the above lists. - You can't add a
Doublebecausefoo3could be pointing at anArrayList<Integer>. - You can't add a
Numberbecausefoo3could be pointing at anArrayList<Integer>. - You can't add an
Objectbecausefoo3could be pointing at anArrayList<Integer>.
- You can add an
PECS
Remember PECS: "Producer Extends, Consumer Super".
-
"Producer Extends" - If you need a
Listto produceTvalues (you want to readTs from the list), you need to declare it with? extends T, e.g.List<? extends Integer>. But you cannot add to this list. -
"Consumer Super" - If you need a
Listto consumeTvalues (you want to writeTs into the list), you need to declare it with? super T, e.g.List<? super Integer>. But there are no guarantees what type of object you may read from this list. -
If you need to both read from and write to a list, you need to declare it exactly with no wildcards, e.g.
List<Integer>.
Example
Note this example from the Java Generics FAQ. Note how the source list src (the producing list) uses extends, and the destination list dest (the consuming list) uses super:
public class Collections {
public static <T> void copy(List<? super T> dest, List<? extends T> src) {
for (int i = 0; i < src.size(); i++)
dest.set(i, src.get(i));
}
}
Also see How can I add to List<? extends Number> data structures?
Reference:
https://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java
https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:JAVA内存区域
下一篇:javaDay03_3
- Codewars Solution:Get the Middle Character 2020-05-21
- 第六章第二十题(计算一个字符串中字母的个数)(Count the l 2020-05-09
- 解决Maven打包时报错"The packaging for this proj 2020-04-22
- java.sql.SQLException: The server time zone value &# 2020-04-22
- @RequestHeader 的使用 2020-04-20
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
