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接口则用于一个类的扩展排序

posted @ 2020-08-26 17:07  mingmingn  阅读(217)  评论(0)    收藏  举报