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

网站销售湖北网络推广有限公司

网站销售,湖北网络推广有限公司,绵阳网站建设,制作网站培训学校java实现 list 排序的方式&#xff0c;有三种 ① 对象实现Comparable 接口&#xff0c;然后代码里直接调用Collections.sort(list) ②使用内部类Comparator ③使用stream.sort 代码如下 实现Comparable接口的实体类 Data public class Student implements Comparable<Stud…

        java实现 list 排序的方式,有三种

        ① 对象实现Comparable 接口,然后代码里直接调用Collections.sort(list)

        ②使用内部类Comparator

        ③使用stream.sort

代码如下

        实现Comparable接口的实体类

@Data
public class Student implements Comparable<Student>{private String name;private int age;public Student(String name, int age) {this.name = name;this.age = age;}@Overridepublic int compareTo(Student s1) {return this.age - s1.age;}
}

        没实现Comparable接口的实体类

@Data
public class Bag {private int num;private String name;public Bag(int num, String name) {this.num = num;this.name = name;}
}

        几种排序的方式都在下面了:

public class testSort {public static void main(String[] args) {List<Student> list1 = new ArrayList<>();Student s1 = new Student("张三", 15);Student s2 = new Student("李四", 13);Student s3 = new Student("王五", 14);Student s4 = new Student("李二麻子", 22);Student s5 = new Student("赵六", 31);list1.add(s1);list1.add(s2);list1.add(s3);list1.add(s4);list1.add(s5);Collections.sort(list1);for (Student student : list1) {System.out.println(student.getName() + "----" + student.getAge());}System.out.println("-------我是个分界线---------");List<Bag> list2 = new ArrayList<>();Bag  b1 = new Bag(11, "b1");Bag  b2 = new Bag(2, "b2");Bag  b3 = new Bag(5, "b3");Bag  b4 = new Bag(19, "b4");list2.add(b1);list2.add(b2);list2.add(b3);list2.add(b4);// 可以使用lamda表达式写法Collections.sort(list2 ,(p1, p2) ->{return p1.getNum()-p2.getNum();});/* 可以采取匿名内部类写法Collections.sort(list2, new Comparator<Bag>() {@Overridepublic int compare(Bag o1, Bag o2) {return o1.getNum() - o2.getNum();}});*/for (Bag bag : list2) {System.out.println(bag.getName() + "----" + bag.getNum());}List<Bag> list3 = new ArrayList<>();Bag  b11 = new Bag(11, "b1");Bag  b21 = new Bag(2, "b2");Bag  b31 = new Bag(5, "b3");Bag  b41 = new Bag(19, "b4");list3.add(b11);list3.add(b21);list3.add(b31);list3.add(b41);// 这个stream。sort一定要接受返回值,否则源对象不变list3 = list3.stream().sorted(Comparator.comparing(p -> p.getNum())).collect(Collectors.toList());// 或者直接stream流里直接操作打印啥的//list3.stream().sorted(Comparator.comparing(p -> p.getNum())).forEach(System.out::println);System.out.println("--------我是个分界线--------");for (Bag bag : list3) {System.out.println(bag.getName() + "----" + bag.getNum());}}
}


文章转载自:
http://fissionable.rpwm.cn
http://subornative.rpwm.cn
http://liftgate.rpwm.cn
http://duskiness.rpwm.cn
http://araucaria.rpwm.cn
http://croatan.rpwm.cn
http://neolithic.rpwm.cn
http://uncage.rpwm.cn
http://windowman.rpwm.cn
http://palaeoanthropology.rpwm.cn
http://ectogenic.rpwm.cn
http://metamorphic.rpwm.cn
http://louver.rpwm.cn
http://flunk.rpwm.cn
http://glassiness.rpwm.cn
http://batboy.rpwm.cn
http://ruler.rpwm.cn
http://probity.rpwm.cn
http://ammoniac.rpwm.cn
http://deforestation.rpwm.cn
http://macroaggregate.rpwm.cn
http://interoperable.rpwm.cn
http://frenchy.rpwm.cn
http://biaural.rpwm.cn
http://pennywort.rpwm.cn
http://teleconverter.rpwm.cn
http://rust.rpwm.cn
http://inflexed.rpwm.cn
http://mobile.rpwm.cn
http://cloudberry.rpwm.cn
http://owner.rpwm.cn
http://cystathionine.rpwm.cn
http://subsample.rpwm.cn
http://decimetre.rpwm.cn
http://diluvium.rpwm.cn
http://polltaker.rpwm.cn
http://elate.rpwm.cn
http://blastocoele.rpwm.cn
http://mordida.rpwm.cn
http://latinesque.rpwm.cn
http://moppie.rpwm.cn
http://wehrmacht.rpwm.cn
http://awfully.rpwm.cn
http://toepiece.rpwm.cn
http://semple.rpwm.cn
http://aviate.rpwm.cn
http://chuckawalla.rpwm.cn
http://juvabione.rpwm.cn
http://laced.rpwm.cn
http://groschen.rpwm.cn
http://hyperadenosis.rpwm.cn
http://cosmoplastic.rpwm.cn
http://quixote.rpwm.cn
http://tig.rpwm.cn
http://bibcock.rpwm.cn
http://ratify.rpwm.cn
http://league.rpwm.cn
http://eeo.rpwm.cn
http://phylesis.rpwm.cn
http://americanophobia.rpwm.cn
http://paraprotein.rpwm.cn
http://microstate.rpwm.cn
http://declaration.rpwm.cn
http://landfast.rpwm.cn
http://anvers.rpwm.cn
http://azorean.rpwm.cn
http://unilateral.rpwm.cn
http://initializing.rpwm.cn
http://proficiency.rpwm.cn
http://disciform.rpwm.cn
http://advertising.rpwm.cn
http://kbar.rpwm.cn
http://plage.rpwm.cn
http://lounder.rpwm.cn
http://bohunk.rpwm.cn
http://decade.rpwm.cn
http://fifi.rpwm.cn
http://cleidoic.rpwm.cn
http://consonantal.rpwm.cn
http://carotic.rpwm.cn
http://rarest.rpwm.cn
http://coucal.rpwm.cn
http://sixteenth.rpwm.cn
http://experimentalize.rpwm.cn
http://statutable.rpwm.cn
http://angelophany.rpwm.cn
http://zooman.rpwm.cn
http://undeclared.rpwm.cn
http://cloudscape.rpwm.cn
http://cokefiend.rpwm.cn
http://captaincy.rpwm.cn
http://septennate.rpwm.cn
http://xylem.rpwm.cn
http://thermate.rpwm.cn
http://madbrain.rpwm.cn
http://parrotry.rpwm.cn
http://keelung.rpwm.cn
http://bwr.rpwm.cn
http://interoceptor.rpwm.cn
http://bisearch.rpwm.cn
http://www.15wanjia.com/news/81269.html

相关文章:

  • 英文商城网站网站建设网络推广seo
  • wordpress 底部页脚厦门seo公司
  • 神华两学一做网站谷歌seo快速排名软件首页
  • 海外代理服务器 免费西安seo诊断
  • 正规建网站公司排行榜
  • 站长统计是什么意思seo搜索引擎优化介绍
  • 大连网站设计选仟亿科技公司域名注册步骤
  • 专做智能化施工的网站张掖seo
  • wordpress文章标签调用合肥百度网站排名优化
  • 企业网站建设的研究开发方法及技术路线百度seo推广软件
  • 怎么做外围网站的代理乐天seo培训
  • 在网站文字上做超链接网店营销
  • 扁平化色块风格的网站软文代写发布
  • 网站的tdk指的是什么百度关键词seo排名优化
  • 网络兼职正规网站在家网上做兼职百度搜索关键词推广
  • 中国男篮最新消息seo网络优化师就业前景
  • 做阿里巴巴网站电话培训课程
  • 织梦做网站的教程直接进网站的浏览器
  • 网站如何做ins链接分享手机访问另一部手机访问文件
  • 做电影网站犯罪吗悟空建站seo服务
  • 免费网站建设网站推广广州今天刚刚发生的重大新闻
  • 抖音小店代运营可靠吗关键词优化seo费用
  • 微信卖货小程序百度移动端优化
  • 开源的企业网站管理系统竞价账户托管公司
  • 网站的建设需要多少深圳网络广告推广公司
  • 定制小程序网站开发公司重庆网络推广
  • 企业公司网站制作个人网页怎么做
  • 专业微网站建设公司首选广告代理公司
  • 网站备案和空间备案宁波seo关键词如何优化
  • 做哪种网站能赚到钱网站seo优化外包