一 概述 1.Comparable與Comparator使用背景 數值型數據(byte int short long float double)天生可對比大小,可排序,String實現了Comparable介面也可以對比大小與排序,而自定義類多種多樣,沒有一個共有的可以用作排序的指標,因此需要在自定 ...
一 概述
1.Comparable與Comparator使用背景
數值型數據(byte int short long float double)天生可對比大小,可排序,String實現了Comparable介面也可以對比大小與排序,而自定義類多種多樣,沒有一個共有的可以用作排序的指標,因此需要在自定義類中手動建立對比的方法,出於這個目的,java提供了兩個介面Comparable與Comparator。
2.集合排序
Collections.sort()底層排序依靠的是Arrays.sort(),而Arrays.sort()排序時採用的是冒泡法。
二 Comparable
需要對比大小的對象可以實現Comparable介面,實現其中的抽象方法,該抽象方法用來設定比較的方式。下麵以一個示例進行說明:
1.實體類
package com.javase.collections.comparable; public class Student implements Comparable<Student> { private String name; private int score; public Student() { super(); } public Student(String name, int score) { super(); this.name = name; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } @Override public int compareTo(Student stu) { return this.score - stu.score;// 操作對象減去參數對象,升序排列,反之降序。 } }
在compareTo()方法中,以屬性score為排序指標,採用“this.score-stu.score”,最終結果以升序排列,反之降序。
2.測試類
package com.javase.collections.comparable; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.junit.Test; public class ComparableTest { @Test public void testComparable() { List<Student> stus = new ArrayList<Student>(); Student zhangsan = new Student("zhangsan", 100); Student lisi = new Student("lisi", 90); Student wanger = new Student("wanger", 95); stus.add(zhangsan); stus.add(lisi); stus.add(wanger); System.out.println("排序前"); for (Student x : stus) { System.out.println(x.getName() + "::" + x.getScore()); } System.out.println("排序後"); Collections.sort(stus); for (Student x : stus) { System.out.println(x.getName() + "::" + x.getScore()); } } }
輸出:
三 Comparator
如果一個類在創建時未實現Comparable介面,希望在不修改源碼的情況下對其對象進行排序,可以在調用排序方法時實現Comparator比較器介面,指定排序方法。下麵以一個示例進行說明:
1.實體類
package com.javase.collections.comparator; public class Student { private String name; private int score; public Student() { super(); } public Student(String name, int score) { super(); this.name = name; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
2.測試類
package com.javase.collections.comparator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.junit.Test; public class ComparatorTest { @Test public void test() { List<Student> stus = new ArrayList<Student>(); Student zhangsan = new Student("zhangsan", 100); Student lisi = new Student("lisi", 90); Student wanger = new Student("wanger", 95); stus.add(zhangsan); stus.add(lisi); stus.add(wanger); System.out.println("排序前"); for (Student x : stus) { System.out.println(x.getName() + "::" + x.getScore()); } System.out.println("-----------------------"); Collections.sort(stus, new Comparator<Student>() { @Override public int compare(Student stu01, Student stu02) { // return stu01.getScore() - stu02.getScore();//升序 return stu02.getScore() - stu01.getScore();// 降序 } }); System.out.println("排序後"); for (Student x : stus) { System.out.println(x.getName() + "::" + x.getScore()); } } }
在compare(Student stu01, Student stu02)方法中,以屬性score為排序指標,採用“stu01.score-stu02.score”,最終結果升序排列,反之降序。
輸出:
參考:
http://www.tiantianbianma.com/java-comparable-comparator.html/