恕我直言你可能真的不會java第7篇:像使用SQL一樣排序集合

字母哥部落格發表於2020-06-28

在開始之前,我先賣個關子提一個問題:我們現在有一個Employee員工類。

@Data
@AllArgsConstructor
public class Employee {

   private Integer id;
   private Integer age;   //年齡
   private String gender;  //性別
   private String firstName;  
   private String lastName;
}

你知道怎麼對一個Employee物件組成的List集合,先按照性別欄位倒序排序,再按照年齡的倒序進行排序麼?如果您不知道4行程式碼以內的解決方案(其實是1行程式碼就可以實現,但筆者格式化為4行),我覺得您有必要一步步的看下去。

一、字串List排序

cities是一個字串陣列。注意london的首字母是小寫的。

List<String> cities = Arrays.asList(
        "Milan",
        "london",
        "San Francisco",
        "Tokyo",
        "New Delhi"
);
System.out.println(cities);
//[Milan, london, San Francisco, Tokyo, New Delhi]

cities.sort(String.CASE_INSENSITIVE_ORDER);
System.out.println(cities);
//[london, Milan, New Delhi, San Francisco, Tokyo]

cities.sort(Comparator.naturalOrder());
System.out.println(cities);
//[Milan, New Delhi, San Francisco, Tokyo, london]
  • 當使用sort方法,按照String.CASE_INSENSITIVE_ORDER(字母大小寫不敏感)的規則排序,結果是:[london, Milan, New Delhi, San Francisco, Tokyo]
  • 如果使用Comparator.naturalOrder()字母自然順序排序,結果是:[Milan, New Delhi, San Francisco, Tokyo, london]

同樣我們可以把排序器Comparator用在Stream管道流中。

cities.stream().sorted(Comparator.naturalOrder()).forEach(System.out::println);

//Milan
//New Delhi
//San Francisco
//Tokyo
//london

在java 7我們是使用Collections.sort()接受一個陣列引數,對陣列進行排序。在java 8之後可以直接呼叫集合類的sort()方法進行排序。sort()方法的引數是一個比較器Comparator介面的實現類,Comparator介面的我們下一節再給大家介紹一下。

二、整數型別List排序

List<Integer> numbers = Arrays.asList(6, 2, 1, 4, 9);
System.out.println(numbers); //[6, 2, 1, 4, 9]

numbers.sort(Comparator.naturalOrder());  //自然排序
System.out.println(numbers); //[1, 2, 4, 6, 9]

numbers.sort(Comparator.reverseOrder()); //倒序排序
System.out.println(numbers);  //[9, 6, 4, 2, 1]

三、按物件欄位對List<Object>排序

這個功能就比較有意思了,舉個例子大家理解一下。

Employee e1 = new Employee(1,23,"M","Rick","Beethovan");
Employee e2 = new Employee(2,13,"F","Martina","Hengis");
Employee e3 = new Employee(3,43,"M","Ricky","Martin");
Employee e4 = new Employee(4,26,"M","Jon","Lowman");
Employee e5 = new Employee(5,19,"F","Cristine","Maria");
Employee e6 = new Employee(6,15,"M","David","Feezor");
Employee e7 = new Employee(7,68,"F","Melissa","Roy");
Employee e8 = new Employee(8,79,"M","Alex","Gussin");
Employee e9 = new Employee(9,15,"F","Neetu","Singh");
Employee e10 = new Employee(10,45,"M","Naveen","Jain");


List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);

employees.sort(Comparator.comparing(Employee::getAge));
employees.forEach(System.out::println);
  • 首先,我們建立了10個Employee物件,然後將它們轉換為List
  • 然後重點的的程式碼:使用了函式應用Employee::getAge作為物件的排序欄位,即使用員工的年齡作為排序欄位
  • 然後呼叫List的forEach方法將List排序結果列印出來,如下(當然我們重寫了Employee的toString方法,不然列印結果沒有意義):
Employee(id=2, age=13, gender=F, firstName=Martina, lastName=Hengis)
Employee(id=6, age=15, gender=M, firstName=David, lastName=Feezor)
Employee(id=9, age=15, gender=F, firstName=Neetu, lastName=Singh)
Employee(id=5, age=19, gender=F, firstName=Cristine, lastName=Maria)
Employee(id=1, age=23, gender=M, firstName=Rick, lastName=Beethovan)
Employee(id=4, age=26, gender=M, firstName=Jon, lastName=Lowman)
Employee(id=3, age=43, gender=M, firstName=Ricky, lastName=Martin)
Employee(id=10, age=45, gender=M, firstName=Naveen, lastName=Jain)
Employee(id=7, age=68, gender=F, firstName=Melissa, lastName=Roy)
Employee(id=8, age=79, gender=M, firstName=Alex, lastName=Gussin)
  • 如果我們希望List按照年齡age的倒序排序,就使用reversed()方法。如:
employees.sort(Comparator.comparing(Employee::getAge).reversed());

四、Comparator鏈對List<Object>排序

下面這段程式碼先是按性別的倒序排序,再按照年齡的倒序排序。

employees.sort(
        Comparator.comparing(Employee::getGender)
        .thenComparing(Employee::getAge)
        .reversed()
);
employees.forEach(System.out::println);

//都是正序 ,不加reversed
//都是倒序,最後面加一個reserved
//先是倒序(加reserved),然後正序
//先是正序(加reserved),然後倒序(加reserved)

細心的朋友可能注意到:我們只用了一個reversed()倒序方法,這個和SQL的表述方式不太一樣。這個問題不太好用語言描述,建議大家去看一下本文對應的視訊

排序結果如下:

Employee(id=8, age=79, gender=M, firstName=Alex, lastName=Gussin)
Employee(id=10, age=45, gender=M, firstName=Naveen, lastName=Jain)
Employee(id=3, age=43, gender=M, firstName=Ricky, lastName=Martin)
Employee(id=4, age=26, gender=M, firstName=Jon, lastName=Lowman)
Employee(id=1, age=23, gender=M, firstName=Rick, lastName=Beethovan)
Employee(id=6, age=15, gender=M, firstName=David, lastName=Feezor)
Employee(id=7, age=68, gender=F, firstName=Melissa, lastName=Roy)
Employee(id=5, age=19, gender=F, firstName=Cristine, lastName=Maria)
Employee(id=9, age=15, gender=F, firstName=Neetu, lastName=Singh)
Employee(id=2, age=13, gender=F, firstName=Martina, lastName=Hengis)

歡迎關注我的部落格,裡面有很多精品合集

  • 本文轉載註明出處(必須帶連線,不能只轉文字):字母哥部落格

覺得對您有幫助的話,幫我點贊、分享!您的支援是我不竭的創作動力! 。另外,筆者最近一段時間輸出瞭如下的精品內容,期待您的關注。

相關文章