Stream的排序

℡╲_俬逩灬. 提交于 2019-12-04 15:38:26

 

1、list<Integer>的正序

复制代码
List<Integer> list = new ArrayList<>();list.add(50);list.add(45);list.add(25);list.add(98);list.add(32);List<Integer> collect = list.stream().sorted().collect(Collectors.toList());System.out.println("list<Integer>元素正序:" + collect);打印结果:list<Integer>元素正序:[25, 32, 45, 50, 98]
复制代码

2、list<Integer>的倒序

复制代码
List<Integer> list = new ArrayList<>();list.add(50);list.add(45);list.add(45);list.add(98);list.add(32);List<Integer> collect = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());System.out.println("list<Integer>元素倒序:" + collect);打印结果:list<Integer>元素倒序:[98, 50, 45, 45, 32]
复制代码

3、List<Person>的元素的属性值正序

复制代码
Person p1 = new Person("张三", new BigDecimal("50.0"));Person p2 = new Person("王五", new BigDecimal("25.0"));Person p3 = new Person("李四", new BigDecimal("68.0"));Person p4 = new Person("李四", new BigDecimal("17.0"));Person p5 = new Person("张三", new BigDecimal("45.0"));List<Person> list = new ArrayList<>();list.add(p1);list.add(p2);list.add(p3);list.add(p4);list.add(p5);list.sort(Comparator.comparing(person -> person.getSalary()));System.out.println(list);打印结果:元素的属性值正序:[{name='李四', salary=17.0}, {name='王五', salary=25.0}, {name='张三', salary=45.0}, {name='张三', salary=50.0}, {name='李四', salary=68.0}]
复制代码

4、List<Person>的元素的属性值倒序

复制代码
Person p1 = new Person("张三", new BigDecimal("50.0"));Person p2 = new Person("王五", new BigDecimal("25.0"));Person p3 = new Person("李四", new BigDecimal("68.0"));Person p4 = new Person("李四", new BigDecimal("17.0"));Person p5 = new Person("张三", new BigDecimal("45.0"));List<Person> list = new ArrayList<>();list.add(p1);list.add(p2);list.add(p3);list.add(p4);list.add(p5);List<Person> collect = list.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).collect(Collectors.toList());System.out.println("元素的属性值倒序:" + collect);打印结果:元素的属性值倒序:[{name='李四', salary=68.0}, {name='张三', salary=50.0}, {name='王五', salary=45.0}, {name='张三', salary=45.0}, {name='李四', salary=17.0}]

 

1、list<Integer>的正序

复制代码
List<Integer> list = new ArrayList<>();list.add(50);list.add(45);list.add(25);list.add(98);list.add(32);List<Integer> collect = list.stream().sorted().collect(Collectors.toList());System.out.println("list<Integer>元素正序:" + collect);打印结果:list<Integer>元素正序:[25, 32, 45, 50, 98]
复制代码

2、list<Integer>的倒序

复制代码
List<Integer> list = new ArrayList<>();list.add(50);list.add(45);list.add(45);list.add(98);list.add(32);List<Integer> collect = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());System.out.println("list<Integer>元素倒序:" + collect);打印结果:list<Integer>元素倒序:[98, 50, 45, 45, 32]
复制代码

3、List<Person>的元素的属性值正序

复制代码
Person p1 = new Person("张三", new BigDecimal("50.0"));Person p2 = new Person("王五", new BigDecimal("25.0"));Person p3 = new Person("李四", new BigDecimal("68.0"));Person p4 = new Person("李四", new BigDecimal("17.0"));Person p5 = new Person("张三", new BigDecimal("45.0"));List<Person> list = new ArrayList<>();list.add(p1);list.add(p2);list.add(p3);list.add(p4);list.add(p5);list.sort(Comparator.comparing(person -> person.getSalary()));System.out.println(list);打印结果:元素的属性值正序:[{name='李四', salary=17.0}, {name='王五', salary=25.0}, {name='张三', salary=45.0}, {name='张三', salary=50.0}, {name='李四', salary=68.0}]
复制代码

4、List<Person>的元素的属性值倒序

复制代码
Person p1 = new Person("张三", new BigDecimal("50.0"));Person p2 = new Person("王五", new BigDecimal("25.0"));Person p3 = new Person("李四", new BigDecimal("68.0"));Person p4 = new Person("李四", new BigDecimal("17.0"));Person p5 = new Person("张三", new BigDecimal("45.0"));List<Person> list = new ArrayList<>();list.add(p1);list.add(p2);list.add(p3);list.add(p4);list.add(p5);List<Person> collect = list.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).collect(Collectors.toList());System.out.println("元素的属性值倒序:" + collect);打印结果:元素的属性值倒序:[{name='李四', salary=68.0}, {name='张三', salary=50.0}, {name='王五', salary=45.0}, {name='张三', salary=45.0}, {name='李四', salary=17.0}]

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!