测试类如下
package com; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class StreamOrderTest { public static void main(String[] args) { User user1 = new User("a",10,999.0); User user2 = new User("b",11,300.0); User user3 = new User("c",12,null); User user4 = new User("d",null,100.0); List得出结论:list = new ArrayList<>(); list.add(user4); list.add(new User("b",11,100.0)); list.add(user2); list.add(user1); list.add(user3); list.add(new User("e",null,200.0)); list.add(new User("e",null,50.0)); System.out.println(list);//[User{name='c', age=null}, User{name='b', age=11}, User{name='a', age=10}, User{name='c', age=12}] //1、对年龄进行升序排序,并且age为null的对象会放到最后 List ageOrderList = list.stream().sorted( Comparator.comparing(User::getAge, Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList()); System.out.println(ageOrderList);//[User{name='a', age=10}, User{name='b', age=11}, User{name='c', age=12}, User{name='c', age=null}] //2、对年龄进行降序排序 List ageOrderDescList = list.stream().sorted( Comparator.comparing(User::getAge,Comparator.nullsFirst(Integer::compareTo)).reversed()).collect(Collectors.toList()); System.out.println(ageOrderDescList); //2、对年龄进行降序排序 第二种写法 但都会把null放前面了.用nullsFirst解决! List ageOrderDescList2 = list.stream().sorted( Comparator.comparing(User::getAge,Comparator.nullsFirst(Integer::compareTo).reversed())).collect(Collectors.toList()); System.out.println(ageOrderDescList2); //3、对年龄进行降序排序,并且age为null的对象会放到最后后 再对财富进行排序,包括年龄为null的那些对象 List ageWealthOrderList = list.stream().sorted( Comparator.comparing(User::getAge, Comparator.nullsLast(Integer::compareTo)) .thenComparing(User::getWealth, Comparator.nullsLast(Double::compareTo).reversed())) .collect(Collectors.toList()); System.out.println(ageWealthOrderList);//[User{name='a', age=10, wealth=999.0}, User{name='b', age=11, wealth=100.0}, User{name='b', age=11, wealth=300.0}, User{name='c', age=12, wealth=null}, User{name='e', age=null, wealth=50.0}, User{name='d', age=null, wealth=100.0}, User{name='e', age=null, wealth=200.0}] } static class User{ private String name; private Integer age; private Double wealth; public User(String name, Integer age) { this.name = name; this.age = age; } public User(String name, Integer age, Double wealth) { this.name = name; this.age = age; this.wealth = wealth; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Double getWealth() { return wealth; } public void setWealth(Double wealth) { this.wealth = wealth; } @Override public String toString() { return "User{" + "name='" + name + ''' + ", age=" + age + ", wealth=" + wealth + '}'; } } }
- Stream表达式可以对某个字段进行升序排序且默认就是升序,为null的对象会放在最后。
- 可以追加多字段排序
- 要想降序排序且null还是在最后,可用nullsFirst认为先把null放在前面,再reversed()则null还是再最后且是按照这个字段降序排序了。
- 注意.reversed()的位置,如果在多字段的整个Comparator外面则会整体倒序!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)