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

上海网站建设设网站关键词排名外包

上海网站建设设,网站关键词排名外包,百度关键词规划师,三门峡市住房和城乡建设局网站在看 Alibaba 开发手册时遇到 PECS 原则,刚开始阅读时感觉比较绕,也搜索了一些博文参考,个人觉得 Stackoverflow 的这篇文章比较实用 —— What is PECS (Producer Extends Consumer Super)? 后面结合 JDK 源码梳理了下 // java/util/List…

在这里插入图片描述
在看 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.mmbaike.com/news/40942.html

相关文章:

  • 鄞州区优秀营销型网站建设首选上海疫情最新消息
  • 网站建设 青岛百度推广收费
  • 安庆做网站电话沈阳黄页88企业名录
  • 全球疫情最新数据统计排名网站优化检测工具
  • 大丰做网站需要多少钱百度贴吧官网首页
  • 公司外贸网站建设兰州怎么提高网站的排名
  • 企业网站源码 可去版权最新seo自动优化软件
  • 沈阳网站建设信息自贡网站seo
  • 安徽省建设厅网站证书查询快照网站
  • dns劫持网站怎么做新闻源软文发布平台
  • ps做网站首页步骤手机优化器
  • 做商城网站要什么手续南昌seo数据监控
  • 做外贸哪个网站比较好淘宝关键词怎么做排名靠前
  • 东莞网站建设企业怎么写软文
  • 安卓系统是谁开发的广州seo实战培训
  • 电商培训课程虞城seo代理地址
  • wordpress 多用户seo网络优化是什么意思
  • 上海网站推广价格网络优化的基本方法
  • 腾讯的网站是谁做的网络营销策划书范文模板
  • u盘搭建网站开发环境方法谷歌外贸平台推广需要多少钱
  • 工作室网站建设费用价格深圳网络推广服务是什么
  • 小说网站建设个人网站制作多少钱
  • 代做网站转账截图十大销售管理软件排行榜
  • 重庆的企业的网站建设沈阳百度seo排名优化软件
  • 南宁做网站哪家好百度竞价排名推广
  • 中国建设网官方网站企业登录推广引流方法与渠道
  • apple开发者账号seo关键词优化外包
  • 网站后台发了文章看不到微信seo
  • 精彩 网站小程序开发公司十大排名
  • 深圳网站建设gseo指什么