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
  1. Reading - Given the above possible assignments, what type of object are you guaranteed to read from List foo3:

    • You can read a Number because any of the lists that could be assigned to foo3 contain a Number or a subclass of Number.
    • You can't read an Integer because foo3 could be pointing at a List<Double>.
    • You can't read a Double because foo3 could be pointing at a List<Integer>.
  2. Writing - Given the above possible assignments, what type of object could you add to List foo3 that would be legal for all the above possible ArrayList assignments:

    • You can't add an Integer because foo3 could be pointing at a List<Double>.
    • You can't add a Double because foo3 could be pointing at a List<Integer>.
    • You can't add a Number because foo3 could be pointing at a List<Integer>.

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
  1. 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 Integer because foo3 could be pointing at a List<Number>or List<Object>.
    • You aren't guaranteed a Number because foo3 could be pointing at a List<Object>.
    • The only guarantee is that you will get an instance of an Object or subclass of Object(but you don't know what subclass).
  2. Writing - Given the above possible assignments, what type of object could you add to List foo3 that would be legal for all the above possible ArrayList assignments:

    • You can add an Integer because an Integer is allowed in any of above lists.
    • You can add an instance of a subclass of Integer because an instance of a subclass of Integer is allowed in any of the above lists.
    • You can't add a Double because foo3 could be pointing at an ArrayList<Integer>.
    • You can't add a Number because foo3 could be pointing at an ArrayList<Integer>.
    • You can't add an Object because foo3 could be pointing at an ArrayList<Integer>.

PECS

Remember PECS"Producer Extends, Consumer Super".

  • "Producer Extends" - If you need a List to produce T values (you want to read Ts 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 List to consume T values (you want to write Ts 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?

From:https://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java

Java PECS相关推荐

  1. JAVA PECS法则

    什么是PECS?PECS指"Producer Extends,Consumer Super".换句话说,如果参数化类型表示一个生产者,就使用<? extends T>: ...

  2. Java泛型中的PECS原则

    今天在写代码的时候使用到了这样一个方法签名: public void foo(Map<String, String> map); 在写这个参数的时候正好在想一些关于泛型的东西,于是: pu ...

  3. java pecs_Java 泛型: 什么是PECS(Producer Extends, Consumer Super)

    什么是PECS? PECS指"Producer Extends,Consumer Super".换句话说,如果参数化类型表示一个生产者,就使用:如果它表示一个消费者,就使用,可能你 ...

  4. Java集合泛型作为参数时,使用中的一些问题。包括但不限于PECS原则

    目录 泛型中的PECS原则以及使用注意 一.泛型中的型变(协变.逆变.不可变) 1. 什么是型变 2. 什么是协变(Covariance) 3. 什么是逆变(Contravariance) 4. 不可 ...

  5. java pecs_Java 泛型 PECS

    在stackoverflow上看到两篇关于java泛型 PECS 的问答: PECS Remember PECS:"Producer Extends,Consumer Super" ...

  6. java pecs_JAVA的PECS原则

    https://segmentfault.com/a/1190000017509439 JAVA的PECS原则 PECS指"Producer Extends,Consumer Super&q ...

  7. java pecs_『Java』泛型中的PECS原则

    Java编程中有时我们要用到不确定的元素,通常用通配符"?"表示,其中" ? extends T "叫"上界通配符", " ? s ...

  8. java 泛型 PECS准则

      我们知道<?>表示:我想使用Java泛型来编写代码,而不是用原生类型:但是在当前这种情况下,我并不能确定下泛型参数的具体类型,因此用?表示任何某种类型.因此,根据我们对通配符的了解,使 ...

  9. Java 泛型的读写规则:PECS

    PECS 是 "Producer Extends Consumer Super" 的缩写,是 Java 泛型中的重要用法. PECS 就是当你需要遍历某一个类型和子类的集合数据时, ...

最新文章

  1. 从创业公司到AI巨头 出门问问如何定义下一代人机交互?
  2. 2.6_Database Interface JDBC及驱动类型
  3. [ASP.NET Core 3框架揭秘] 依赖注入:依赖注入模式
  4. JavaFX UI控件教程(七)之Checkbox
  5. 洛谷 P1968 美元汇率
  6. 如何修复XML内存“泄漏”
  7. offsetTop和scrollTop差异
  8. spring与jdk版本要求
  9. 监督学习的基本假设——联合概率分布,独立同分布
  10. 个人网站搭建时linux中的相关配置记录(mysql,jdk,nginx,redis)
  11. 如何将matlab设置为默认打开方式,如何设置默认打开方式
  12. 通过wifi进行adb远程连接手机进行调试
  13. 随堂笔记4——文本编辑器Vim
  14. 布局福建市场,维也纳酒店欧暇·地中海酒店能否为投资人带来信心与底气?
  15. 债券价格和到期收益率的关系_债券价格、到期收益率与票面利率之间的关系是什么?...
  16. 计算机网络与技术课本,高等学校计算机科学与技术教材:计算机网络基础教程...
  17. 推荐算法实践-章节三-推荐系统冷启动问题-阅读总结
  18. validateJarFile jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending c
  19. 加加魔兽助手Ver7.65去广告方法!
  20. linux java db2,[转载]DB2 UDB for Linux, UNIX 和 Windows 中的 Java 开发概述: V8.1 更新版

热门文章

  1. [POJ1338]Ugly Numbers
  2. CentOS系统配置solr
  3. [转载][记录]javascript生成不重复的随机数
  4. Unity3d Fast Indirect illumination Using Two Virtual Spherical Gaussian Lights-Square Enix论文
  5. Linux Socket TCP/IP通信
  6. 62 | 测一测 | 这些软件测试题目,你都掌握了吗?
  7. linux简单文件管理命令的使用
  8. js 让浏览器全屏模式的方法launchFullscreen
  9. 腾讯一面有感(移动开发岗位)
  10. vim环境设置(应用于python编程)