做用java
區別數組
Comparable使用ide
Comparator的使用函數
總結測試
Java的Comparable和Comparator當須要排序的集合或數組不是單純的數字類型時,一般可使用Comparator或Comparable,以簡單的方式實現對象排序或自定義排序。this
1、Comparable
強行對實現它的每一個類的對象進行總體排序,實現此接口的對象列表(和數組)能夠經過Collections.sort或Arrays.sort進行自動排序。Comparable是一個內比較器,實現了Comparable接口。是能夠和本身比較的。Comparable接口的類的比較,則依賴compareTo方法來實現,compareTo方法也被稱爲天然比較方法。若是add一個Collection的對象想時想要Collections的sort方自動進行排序的話,那麼這個對象必須實現Comparable接口,並重寫compareTo方法。compareTo方法有一個參數,返回值是int,有三種狀況:spa
一、新添加進來的對象實例大於已經存在的對象時,返回正整數 1 。表示不一樣的實例對象,添加至已存在對象的後面。code
二、新添加進來的對象實例等於已經存在的對象時,返回 0。表示重複對象實例,則不被添加。對象
三、新添加進來的對象實例小於已經存在的對象時,返回負整數 -1。表示不一樣的實例對象,添加至已存在對象的前面。排序
2、Comparator
強行對某個對象collection進行總體排序的比較函數,能夠將Comparator傳遞給Collections.sort或Arrays.sort。Comparator是一個外比較器,它的比較則是經過重寫compare方法來實現的。方法有兩個參數T o1和T o2,是泛型的表示方式,分別表示待比較的兩個對象,o1表示要添加的新的對象實例,o2表示已經添加進來的對象實例,返回值也是int,分三種狀況:
一、o1大於o2,返回正整數 1 。表示不一樣的實例對象,添加至已存在對象的後面。
二、o1等於o2,返回 0。表示重複對象實例,則不被添加。
三、o1小於o3,返回負整數 -1。表示不一樣的實例對象,添加至已存在對象的前面。
定義一個類,並實現Comparable接口,重寫compareTo方法,根據返回值編寫本身的比較規則。
具體代碼實現:
package com.vince; public class Animal1 implements Comparable<Animal1>{ private String name; private String color; private Float weight; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Float getWeight() { return weight; } public void setWeight(Float weight) { this.weight = weight; } public Animal1(String name, String color, Float weight) { super(); this.name = name; this.color = color; this.weight = weight; } @Override public String toString() { return "名字:" + name + "\t顏色:" + color + "\t體重:" + weight; } //按照體重對動物升序排列 @Override public int compareTo(Animal1 o) { // 判斷對象是否爲空,爲空則不添加 if(o == null) { return 0; } if(this.weight >= o.getWeight()) { return 1;// 對象不爲空,若是體重大的則排到後面 }else { return -1;// 對象不爲空,若是體重小的則排到前面 } } }
package test; import java.util.Set; import java.util.TreeSet; import com.vince.Animal1; public class Test2 { public static void main(String[] args) { // TODO Set<Animal1> animals = new TreeSet<>(); animals.add(new Animal1("Monkey", "black", 90f)); animals.add(new Animal1("Duck", "yellow", 5f)); animals.add(new Animal1("Chicken", "white", 7f)); animals.add(new Animal1("Goldfish", "golden", 2f)); animals.add(new Animal1("Elephant", "gray", 250f)); System.out.println("內部比較器升序排序後的結果爲:"); for (Animal1 animal : animals) { System.out.println(animal); } } }
匿名的方式實現Comparator:在建立一個類時,不直接在類當中去實現,而是在測試類中採用匿名內部類的方式,直接建立一個Comparator來實現comparable方法。
具體代碼實現:
package com.vince; public class Animal2 { private String name; private String color; private Float weight; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Float getWeight() { return weight; } public void setWeight(Float weight) { this.weight = weight; } public Animal2(String name, String color, Float weight) { super(); this.name = name; this.color = color; this.weight = weight; } @Override public String toString() { return "名字:" + name + "\t顏色:" + color + "\t體重:" + weight; } }
package test; import java.util.Comparator; import java.util.Set; import java.util.TreeSet; import com.vince.Animal2; public class Test3 { public static void main(String[] args) { // 匿名內部類的方式實現比較器降序排序 Set<Animal2> animal2s = new TreeSet<Animal2>(new Comparator<Animal2>() { @Override public int compare(Animal2 o1, Animal2 o2) { // 若是對象都爲空則不添加 if(o1 == null || o2 == null) { return 0; } if(o1.getWeight() >= o2.getWeight()) { return -1;//體重大的放前面 }else { return 1;//體重小的放前面 } } }); animal2s.add(new Animal2("Monkey", "black", 90f)); animal2s.add(new Animal2("Duck", "yellow", 5f)); animal2s.add(new Animal2("Chicken", "white", 7f)); animal2s.add(new Animal2("Goldfish", "golden", 2f)); animal2s.add(new Animal2("Elephant", "gray", 250f)); System.out.println("外部比較器降序排列後的結果爲:"); for (Animal2 animal : animal2s) { System.out.println(animal); } } }
自定義比較器Comparator:另外建立一個比較器類直接在類裏面實現compare方法,在測試類直接new一個Comparator。
具體代碼實現:
package com.vince; public class Student { private Integer classNumber; private String name; private char sex; private Integer age; public Integer getClassNumber() { return classNumber; } public String getName() { return name; } public char getSex() { return sex; } public Integer getAge() { return age; } public Student(Integer classNumber, String name, char sex, Integer age) { super(); this.classNumber = classNumber; this.name = name; this.sex = sex; this.age = age; } @Override public String toString() { return "班級編號:" + getClassNumber() + "\t姓名:" + getName() + "\t性別:" + sex + "\t年齡:" + age; } }
package comparator; import java.util.Comparator; import com.vince.Student; public class StudentComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { // TODO if(o1.getAge() == o2.getAge()) { return 0; } if(o1.getAge() >= o2.getAge()) { return 1; }else { return -1; } } }
package test; import java.util.TreeSet; import com.vince.Student; import comparator.StudentComparator; public class Test4 { public static void main(String[] args) { TreeSet<Student> treeSet = new TreeSet<>(new StudentComparator()); treeSet.add(new Student(1,"小明",'男',22)); treeSet.add(new Student(5,"小白",'男',20)); treeSet.add(new Student(3,"木子",'女',45)); treeSet.add(new Student(3,"木子",'女',45)); treeSet.add(new Student(7,"小綠",'女',32)); treeSet.add(new Student(1,"小明",'男',22)); for (Student student : treeSet) { System.out.println(student); } } }
比較器是把集合或數組的元素強行按照指定方法進行排序的對象,它是實現了Comparator接口類的實例。若是一個集合元素是可比較的(實現了Comparable接口),那麼就具備了默認排序方法,比較器則是強行改變它默認的比較方式來進行排序。或者有的集合元素不可比較(沒有實現Comparable接口),則可用比較器來實現動態的排序。
相同點:Comparable和Comparator能夠按照指定方法,對對象實現動態的排序。他們實現排序的方法的返回值都是int類型,分爲三種狀況:一、0和-1。
不一樣點:Comparable的排序依賴的是compareTo方法,且參數只有一個。Comparator的排序依賴的是compare,傳入參數有兩個,能夠經過匿名內部類或者單獨實現。Comparator接口通常不會被集合元素類所實現,而是單獨實現或者匿名內部類方式實現。