Collections集合工具类的方法_sort(List,Comparator)
簡述Comparable和Comparator兩個接口的區別。
Comparable:強行對實現它的每個類的對象進行整體排序。這種排序被稱為類的自然排序,類的compareTo方法被稱為它的自然比較方法。只能在類中實現compareTo()一次,不能經常修改類的代碼實現自己想要的排序。實現此接口的對象列表(和數組)可以通過Collections.sort(和Arrays.sort)進行自動排序,對象可以用作有序映射中的鍵或有序集合中的元素,無需指定比較器。
Comparator強行對某個對象進行整體排序。可以將Comparator 傳遞給sort方法(如Collections.sort或 Arrays.sort),從而允許在排序順序上實現精確控制。還可以使用Comparator來控制某些數據結構(如有序set或有序映射)的順序,或者為那些沒有自然順序的對象collection提供排序。
練習
創建一個學生類,存儲到ArrayList集合中完成指定排序操作。
Student 初始類
package com.learn.demo05.Collections;public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;} }測試類:
public class Demo {public static void main(String[] args) {// 創建四個學生對象 存儲到集合中ArrayList<Student> list = new ArrayList<Student>();list.add(new Student("rose",18));list.add(new Student("jack",16));list.add(new Student("abc",16));list.add(new Student("ace",17));list.add(new Student("mark",16));/*讓學生 按照年齡排序 升序*/ // Collections.sort(list);//要求 該list中元素類型 必須實現比較器Comparable接口for (Student student : list) {System.out.println(student);}} }發現,當我們調用Collections.sort()方法的時候 程序報錯了。
原因:如果想要集合中的元素完成排序,那么必須要實現比較器Comparable接口。
于是我們就完成了Student類的一個實現,如下:
public class Student implements Comparable<Student>{....@Overridepublic int compareTo(Student o) {return this.age-o.age;//升序} }再次測試,代碼就OK 了效果如下:
Student{name='jack', age=16} Student{name='abc', age=16} Student{name='mark', age=16} Student{name='ace', age=17} Student{name='rose', age=18}擴展
如果在使用的時候,想要獨立的定義規則去使用 可以采用Collections.sort(List list,Comparetor<T> c)方式,自己定義規則:
Collections.sort(list, new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {return o2.getAge()-o1.getAge();//以學生的年齡降序} });效果:
Student{name='rose', age=18} Student{name='ace', age=17} Student{name='jack', age=16} Student{name='abc', age=16} Student{name='mark', age=16}如果想要規則更多一些,可以參考下面代碼:
Collections.sort(list, new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {// 年齡降序int result = o2.getAge()-o1.getAge();//年齡降序if(result==0){//第一個規則判斷完了 下一個規則 姓名的首字母 升序result = o1.getName().charAt(0)-o2.getName().charAt(0);}return result;}});效果如下:
Student{name='rose', age=18} Student{name='ace', age=17} Student{name='abc', age=16} Student{name='jack', age=16} Student{name='mark', age=16} package com.learn.demo05.Collections;import java.util.ArrayList; import java.util.Collections; import java.util.Comparator;/*- java.utils.Collections是集合工具類,用來對集合進行操作。部分方法如下:public static <T> void sort(List<T> list,Comparator<? super T> ):將集合中元素按照指定規則排序。Comparator和Comparable的區別Comparable:自己(this)和別人(參數)比較,自己需要實現Comparable接口,重寫比較的規則compareTo方法Comparator:相當于找一個第三方的裁判,比較兩個Comparator的排序規則:o1-o2:升序*/ public class Demo03Sort {public static void main(String[] args) {ArrayList<Integer> list01 = new ArrayList<>();list01.add(1);list01.add(3);list01.add(2);System.out.println(list01);//[1, 3, 2]Collections.sort(list01, new Comparator<Integer>() {//重寫比較的規則@Overridepublic int compare(Integer o1, Integer o2) {//return o1-o2;//升序return o2-o1;//降序}});System.out.println(list01);ArrayList<Student> list02 = new ArrayList<>();list02.add(new Student("a迪麗熱巴",18));list02.add(new Student("古力娜扎",20));list02.add(new Student("楊冪",17));list02.add(new Student("b楊冪",18));System.out.println(list02);/*Collections.sort(list02, new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {//按照年齡升序排序return o1.getAge()-o2.getAge();}});*///擴展:了解Collections.sort(list02, new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {//按照年齡升序排序int result = o1.getAge()-o2.getAge();//如果兩個人年齡相同,再使用姓名的第一個字比較if(result==0){result = o1.getName().charAt(0)-o2.getName().charAt(0);}return result;}});System.out.println(list02);} }?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的Collections集合工具类的方法_sort(List,Comparator)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Collections集合工具类的方法_
- 下一篇: 函数式接口的概念函数式接口的定义