使用 Stream的sort() 方法,并结合 Comparator 类 进行排序,主要解决以下两个问题:
1)当某个字段为空时,怎么进行排序?---使用 Comparator.nullsFirst() 或者 Comparator.nullsLast( ) 进行非空的剔除
2)如果比较两个字段?---使用 thenComparing( )
1 package com.demrystv.stonestrong.stone.streamTest; 2 3 import com.alibaba.fastjson.JSON; 4 import org.junit.Test; 5 6 import java.util.Arrays; 7 import java.util.Comparator; 8 import java.util.List; 9 import java.util.stream.Collectors; 10 11 public class StreamController { 12 13 @Test 14 public void streamSortTest(){ 15 User user1 = new User(); 16 user1.setAge(36); 17 user1.setUserName("Bryant"); 18 user1.setAddress("LA"); 19 user1.setPhone("11111"); 20 21 User user2 = new User(); 22 user2.setAge(37); 23 user2.setUserName("James"); 24 user2.setPhone("22222"); 25 26 User user3 = new User(); 27 user3.setAge(39); 28 user3.setUserName("Harden"); 29 user3.setAddress("Houston"); 30 user3.setPhone("33333"); 31 32 User user4 = new User(); 33 user4.setAge(28); 34 user4.setUserName("Durant"); 35 user4.setAddress("NY"); 36 37 User user5 = new User(); 38 user5.setAge(29); 39 user5.setUserName("Wade"); 40 user5.setAddress("Miami"); 41 user5.setPhone("55555"); 42 43 44 45 List<User> userList = Arrays.asList(user1, user2, user3, user4, user5); 46 List<User> collect1 = userList.stream().sorted(Comparator.comparing(User::getAddress, Comparator.nullsFirst(String::compareTo))).collect(Collectors.toList()); 47 System.out.println("根据地址正序排序,null的在前面: "); 48 System.out.println(JSON.toJSONString(collect1)); 49 System.out.println("========================="); 50 System.out.println(); 51 52 List<User> collect2 = userList.stream().sorted(Comparator.comparing(User::getAddress, Comparator.nullsLast(String::compareTo))).collect(Collectors.toList()); 53 System.out.println("根据地址正序排序,null的在后面: "); 54 System.out.println(JSON.toJSONString(collect2)); 55 System.out.println("========================="); 56 System.out.println(); 57 58 List<User> collect3 = userList.stream().sorted(Comparator.comparing(User::getAddress, Comparator.nullsFirst(String::compareTo)).reversed()).collect(Collectors.toList()); 59 System.out.println("根据地址逆序排序,null的在后面: "); 60 System.out.println(JSON.toJSONString(collect3)); 61 System.out.println("========================="); 62 System.out.println(); 63 64 List<User> collect4 = userList.stream().sorted(Comparator.comparing(User::getAddress, Comparator.nullsFirst(String::compareTo)). 65 thenComparing(User::getPhone, Comparator.nullsFirst(String::compareTo)).reversed()).collect(Collectors.toList()); 66 System.out.println("先根据地址正序排序,再根据电话逆序排序,null的在后面: "); 67 System.out.println(JSON.toJSONString(collect4)); 68 System.out.println("========================="); 69 System.out.println(); 70 71 } 72 }
运行结果如下:
根据地址正序排序,null的在前面:
[{"age":37,"phone":"22222","userName":"James"},{"address":"Houston","age":39,"phone":"33333","userName":"Harden"},{"address":"LA","age":36,"phone":"11111","userName":"Bryant"},{"address":"Miami","age":29,"phone":"55555","userName":"Wade"},{"address":"NY","age":28,"userName":"Durant"}]
=========================
根据地址正序排序,null的在后面:
[{"address":"Houston","age":39,"phone":"33333","userName":"Harden"},{"address":"LA","age":36,"phone":"11111","userName":"Bryant"},{"address":"Miami","age":29,"phone":"55555","userName":"Wade"},{"address":"NY","age":28,"userName":"Durant"},{"age":37,"phone":"22222","userName":"James"}]
=========================
根据地址逆序排序,null的在后面:
[{"address":"NY","age":28,"userName":"Durant"},{"address":"Miami","age":29,"phone":"55555","userName":"Wade"},{"address":"LA","age":36,"phone":"11111","userName":"Bryant"},{"address":"Houston","age":39,"phone":"33333","userName":"Harden"},{"age":37,"phone":"22222","userName":"James"}]
=========================
先根据地址正序排序,再根据电话逆序排序,null的在后面:
[{"address":"NY","age":28,"userName":"Durant"},{"address":"Miami","age":29,"phone":"55555","userName":"Wade"},{"address":"LA","age":36,"phone":"11111","userName":"Bryant"},{"address":"Houston","age":39,"phone":"33333","userName":"Harden"},{"age":37,"phone":"22222","userName":"James"}]
=========================