1. 程式人生 > 實用技巧 >1-5-3 Java工具類--集合排序

1-5-3 Java工具類--集合排序

集合排序

使用Collections類的sort()方法

sort(List<T> list) -根據元素的自ii然順序對指定列表按升序進行排序.

Comparator介面

 1 public class Cat {
 2     private String name; //名字
 3     private int month; //年齡
 4     private String species;//品種
 5     
 6     //構造方法
 7     public Cat(String name, int month, String species) {
 8         super
(); 9 this.name = name; 10 this.month = month; 11 this.species = species; 12 } 13 //getter與setter方法 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public int
getMonth() { 23 return month; 24 } 25 26 public void setMonth(int month) { 27 this.month = month; 28 } 29 30 public String getSpecies() { 31 return species; 32 } 33 34 public void setSpecies(String species) { 35 this.species = species; 36 }
37 @Override 38 public String toString() { 39 return "[名字:" + name + ", 年齡:" + month + ", 品種:" + species + "]"; 40 } 41 }
 1 import java.util.Comparator;
 2 
 3 public class NameComparator implements Comparator<Cat> {
 4 
 5     @Override
 6     public int compare(Cat o1, Cat o2) {
 7         // 按名字升序排序
 8         String name1=o1.getName();
 9         String name2=o2.getName();
10         int n=name1.compareTo(name2);
11         return n;
12     }
13 
14 }
 1 public class CatTest {
 2 
 3     public static void main(String[] args) {
 4         // 按名字升序排序
 5         Cat huahua=new Cat("huahua",5,"英國短毛貓");
 6         Cat fanfan=new Cat("fanfan",2,"中華田園貓");
 7         Cat maomao=new Cat("maomao",3,"中華田園貓");
 8         List<Cat> catList=new ArrayList<Cat>();
 9         catList.add(huahua);
10         catList.add(fanfan);
11         catList.add(maomao);
12         //排序前
13         System.out.println("排序前:");
14         for(Cat cat:catList){
15             System.out.println(cat);
16         }
17         //按名字進行升序排序
18         Collections.sort(catList, new NameComparator());
19         System.out.println("按名字升序排序後:");
20         for(Cat cat:catList){
21             System.out.println(cat);
22         }
23         //按年齡進行降序排序
24         Collections.sort(catList, new AgeComparator());
25         System.out.println("按年齡降序排序後:");
26         for(Cat cat:catList){
27             System.out.println(cat);
28         }
29     }
30 
31 }

Comparable介面

 1 public class Goods implements Comparable<Goods> {
 2     private String id;//商品編號
 3     private String name;//商品名稱
 4     private double price;//商品價格
 5     //構造方法
 6     public Goods(String id,String name,double price){
 7         this.id=id;
 8         this.name=name;
 9         this.price=price;
10     }
11 
12     //getter和setter方法
13     public String getId() {
14         return id;
15     }
16 
17     public void setId(String id) {
18         this.id = id;
19     }
20 
21     public String getName() {
22         return name;
23     }
24 
25     public void setName(String name) {
26         this.name = name;
27     }
28 
29     public double getPrice() {
30         return price;
31     }
32 
33     public void setPrice(double price) {
34         this.price = price;
35     }
36     public String toString(){
37         return "商品編號:"+id+",商品名稱:"+name+",商品價格:"+price;
38     }
39     @Override
40     public int compareTo(Goods o) {
41         // 取出商品價格
42         double price1=this.getPrice();
43         double price2=o.getPrice();
44         int n=new Double(price2-price1).intValue();
45         return n;
46     }
47 
48 }
 1 import java.util.ArrayList;
 2 import java.util.Collections;
 3 import java.util.List;
 4 
 5 public class GoodsTest {
 6 
 7     public static void main(String[] args) {
 8         Goods g1 = new Goods("s00001", "手機", 2000);
 9         Goods g2 = new Goods("s00002", "冰箱", 5000);
10         Goods g3 = new Goods("s00003", "電視機", 3000);
11         List<Goods> goodsList = new ArrayList<Goods>();
12         goodsList.add(g1);
13         goodsList.add(g2);
14         goodsList.add(g3);
15         // 排序前
16         System.out.println("排序前:");
17         for (Goods goods : goodsList) {
18             System.out.println(goods);
19         }
20         Collections.sort(goodsList);
21         // 排序後
22         System.out.println("排序後:");
23         for (Goods goods : goodsList) {
24             System.out.println(goods);
25         }
26 
27     }
28 
29 }

Comparable和Comparator的區別

1、Comparator介面位於java.util包下,而Comparable介面位於java.lang包下

2、對於Comparator介面,可以看到它的compare()方法的引數是兩個物件,比如我們隊Cat類進行比較,那麼這裡就是兩個要比較的Cat類的物件,所以可以有一個單獨的類實現Comparator

對於Comparable介面,它的方法只有一個物件作為引數,所以要比較的類需要實現Comparable介面,將當前物件與方法引數中的物件進行比較。

3、關於應用場景

一般情況下如果對某個類進行排序,比如Cat類,如果使用Comparable介面的方式,那麼Cat類需要實現Comparable介面。如果Cat類通過Comparable介面的方式實現排序,比如通過name排序了。那麼我們還希望通過age進行排序,這時不希望修改Cat類,那此時就需要使用Comparator介面了

因此,Comparable介面可以作為實現類的預設排序演算法,Comparator介面則用於一個類的擴充套件排序