当前位置: 首页 > news >正文

Java PECS(Producer Extends Consumer Super)原则

在这里插入图片描述
在看 Alibaba 开发手册时遇到 PECS 原则,刚开始阅读时感觉比较绕,也搜索了一些博文参考,个人觉得 Stackoverflow 的这篇文章比较实用 —— What is PECS (Producer Extends Consumer Super)?

后面结合 JDK 源码梳理了下

// java/util/List.class
public interface List<E> extends Collection<E> {......default void sort(Comparator<? super E> c) {Object[] a = this.toArray();Arrays.sort(a, (Comparator) c);ListIterator<E> i = this.listIterator();for (Object e : a) {i.next();i.set((E) e);}}
}

对于 sort 方法,参数 Comparator<? super E> c 是消费者,c 会获取从该集合 List<E> 中获取元素进行比较。比较器使用的是 Comparator<? super E> c ,因为其 int compare(T o1, T o2) 方法中参数类型 T 是 E 的父类,那么从集合 List<E> 中获取的元素任何 E 类型的方法都可以调用该 int compare(T o1, T o2) 方法进行比较。
例如,

public static class Animal {public String name; // The name of the animal
}public static class Cat extends Animal {public int speed; // The Running Speed of Catspublic Cat(String name, int speed) {this.speed = speed;super.name = name;}@Overridepublic String toString() {return name;}
}public static void main(String[] args) {List<Cat> list = Lists.newArrayList(new Cat("Cookie", 3), new Cat("Trifle", 2));// Comparator<Animal> list.sort(Comparator.comparing((Animal o) -> o.name)); // Cookie, Triflelist.forEach(System.err::println);// Comparator<Cat>list.sort(Comparator.comparing((Cat o) -> o.speed)); // Trifle, Cookielist.forEach(System.err::println);
}

对于 List<Cat>sort 时比较器可以为 Comparator<Animal> ,Cat 继承 Animal,可以把 Cat 作为 Animal消费。更甚至比较器可以为 Comparator<Object>

可以想象一下,如果 sort 参数形式为(Comparator<? extends E> c),从 List<E> 中获取元素只能保证为 E 类型,但比较器 int compare(T o1, T o2) 方法中参数类型 T 是 E 的子类,T 中某些操作 E 是不能满足的。

// java/util/List.class
public interface List<E> extends Collection<E> {......boolean addAll(Collection<? extends E> c);
}

addAll 会把参数 Collection<? extends E> c 中的所有元素以其迭代器返回顺序添加到该 List 末尾。

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it’s nonempty.)

对于 addAll 方法,参数 Collection<? extends E> c 是生产者,负责提供向 List<E> 末尾添加的元素。
根据PECS原则,这里应该使用 Collection<? extends E> c 而非 Collection<? super E> c ,这样可以保证从集合 c 中获取的元素都是 E 的子类,任何对 E 进行的操作它都可以满足。

http://www.lryc.cn/news/67619.html

相关文章:

  • Learn RabbitMQ with SpringBoot
  • 定时器 POSIX Timer定时器和setitimer定时器
  • DeSD:用于3D医学图像分割的深度自蒸馏自监督学习
  • MySQL数据库——MySQL创建触发器(CREATE TRIGGER)
  • Java实现网上人才招聘系统【附源码】
  • jmeter接口测试项目实战详解,零基础也能学,源码框架都给你
  • MySQL中去重 distinct 和 group by 是如何去重的
  • 在职读研是理想还是情怀?你想要的都将在社科大能源管理硕士项目实现
  • 携手共建数字钢铁,Hightopo亮相第三届钢铁展洽会
  • Leetcode2383. 赢得比赛需要的最少训练时长
  • js代码执行过程、调用栈、执行上下文
  • 互联网摸鱼日报(2023-05-12)
  • 【Python从入门到实践3.1】扑克发牌知识点(range函数,def函数,else语句配合使用,random库,列表推导式)
  • Spring Cloud第二季--Spring Cloud Bus
  • Unittest自动化测试之unittestunittest_生成测试报告
  • 一个查询IP地理信息和CDN提供商的离线终端工具
  • RflySim平台使用篇 | Rflysim3D软件使用系列教程(二)
  • 2023 年第五届河南省 CCPC 大学生程序设计竞赛
  • nginx liunx最新版本安装flask部署
  • 热图 -- pheatmap or ggplot2
  • EIScopus检索 | 2023年智能交通与未来出行国际会议(CSTFM 2023)
  • 如何系列 如何在Windows和Linux安装Nginx
  • “1+X+N”模式助力企业数字化转型
  • JavaEE(系列3) -- 多线程(线程的中断与线程等待)
  • 想装一台自己的电脑,可以先了解下这些问题
  • Redis未授权漏洞复现
  • 跳槽,如果没有更好的选择,可以去美团试试···
  • Java10
  • IMS call通话类型对比差异
  • 5.2 中心极限定理