比较器Comparable与Comparator

一、Comparable接口

public interfaceComparable<T>

  • 该接口对实现它的每个类的对象强加一个整体排序。 这个排序被称为类的自然排序 ,类的 compareTo方法被称为其自然比较方法 。
  • 使用范例
  • [html]  view plain  copy
    1. import java.lang.reflect.Array;  
    2. import java.util.ArrayList;  
    3. import java.util.Arrays;  
    4. import java.util.LinkedList;  
    5. import java.util.List;  
    6.   
    7. import javafx.scene.transform.Scale;  
    8.   
    9. class Persons implements Comparable<Persons>  
    10. {  
    11.     private String name;  
    12.     private int socer;  
    13.     public Persons(String name,int socer)  
    14.     {  
    15.         this.name=name;  
    16.         this.socer=socer;  
    17.     }  
    18.       
    19.     @Override                     //覆写Object类的toString()方法  
    20.     public String toString()  
    21.     {  
    22.         return "人名:"+this.name+",分数:"+this.socer+"\n";  
    23.     }  
    24.       
    25.     @Override                     //覆写Comparable里的compareTo方法  
    26.     public int compareTo(Persons p)  
    27.     {  
    28.         if(this.socer>p.socer)  
    29.             return -1;  
    30.         else if(this.socer<p.socer)  
    31.             return 1;  
    32.         else  
    33.             return 0;  
    34.     }  
    35. }  
    36. public class comparbleDemo {  
    37.   
    38.     public static void main(String[] args) {  
    39.         List<Persons> ps=new LinkedList<Persons> ();  
    40.         ps.add(new Persons("郑道翔",66));  
    41.         ps.add(new Persons("秦鸿俊",67));  
    42.         ps.add(new Persons("安晨阳",60));  
    43.         ps.add(new Persons("张昊雨",99));  
    44.         Object obj[]=ps.toArray();  
    45.         Arrays.sort(obj);                //为对象数组排序,系统自动根据compareTo方法进行比较  
    46.         System.out.println(Arrays.toString(obj));  
    47.     }  
    48.   
    49. }  
    输出:
  • [人名:张昊雨,分数:99
    , 人名:秦鸿俊,分数:67
    , 人名:郑道翔,分数:66
    , 人名:安晨阳,分数:60
    ]
二、挽救的比较器接口——Comparator接口

假如一个类没有实现Comparable接口,但有一天想为这个类进行排序,这样就会用到Comparator接口。

这就需要我们单独写一个类实现Comparator接口

范例1

[html]  view plain  copy
  1. import java.util.Arrays;  
  2. import java.util.Comparator;  
  3. /*  
  4.  * 有时类没有实现Comparable接口,可以单独写一个实现了Comparator接口的类  
  5.  */  
  6. class Book  
  7. {  
  8.     private String title;  
  9.     private double price;  
  10.     public Book(String title,double price)  
  11.     {  
  12.         this.title=title;  
  13.         this.price=price;  
  14.     }  
  15.     public String getTitle() {  
  16.         return title;  
  17.     }  
  18.     public void setTitle(String title) {  
  19.         this.title = title;  
  20.     }  
  21.     public double getPrice() {  
  22.         return price;  
  23.     }  
  24.     public void setPrice(double price) {  
  25.         this.price = price;  
  26.     }  
  27.     @Override  
  28.     public String toString()  
  29.     {  
  30.         return "书名:"+this.title+",价格:"+this.price+"\n";  
  31.     }  
  32. }  
  33.   
  34. class BookComparator implements Comparator <Book>//Book类没有实现Comparable接口,使用Comparator接口进行比较  
  35. {  
  36.     @Override  
  37.     public int compare(Book o1,Book o2)  
  38.     {  
  39.         if(o1.getPrice()>o2.getPrice())  
  40.             return 1;  
  41.         else if(o1.getPrice()<o2.getPrice())  
  42.             return -1;  
  43.         else  
  44.             return 0;  
  45.     }  
  46. }  
  47.   
  48. public class comparatorDemo {  
  49.   
  50.     public static void main(String[] args) {  
  51.         Book books[] = new Book[] {new Book("算法",34.5),  
  52.                                    new Book("数据结构",44.2),  
  53.                                    new Book("漫画",6.9),  
  54.                                    new Book("三国演义",66.1)  
  55.         };  
  56.         Arrays.sort(books,new BookComparator());   //使用了实现了Comparator接口的BookComparator类进行排序  
  57.         System.out.println(Arrays.toString(books));  
  58.     }  
  59.   
  60. }  
输出:
[书名:漫画,价格:6.9
, 书名:算法,价格:34.5
, 书名:数据结构,价格:44.2
, 书名:三国演义,价格:66.1
]
范例2

[html]  view plain  copy
  1. import java.util.*;  
  2.   
  3. class Student  
  4. {  
  5.     public String getClassnum() {  
  6.         return classnum;  
  7.     }  
  8.     public void setClassnum(String classnum) {  
  9.         this.classnum = classnum;  
  10.     }  
  11.     public String getName() {  
  12.         return name;  
  13.     }  
  14.     public void setName(String name) {  
  15.         this.name = name;  
  16.     }  
  17.     private String classnum;  
  18.     private String name;  
  19.     public Student(String classnum,String name)  
  20.     {  
  21.         this.classnum=classnum;  
  22.         this.name=name;  
  23.     }  
  24.     @Override  
  25.     public String toString()  
  26.     {  
  27.         return "人名:"+this.name+",班级名称:"+this.classnum+"\n";  
  28.     }  
  29. }  
  30.   
  31. class studentcomparator implements Comparator<Student>  
  32. {  
  33.   
  34.     @Override                         //覆写compare()方法,班级一样时比较名字  
  35.     public int compare(Student o1, Student o2) {  
  36.         if(o1.getClassnum().equals(o2.getClassnum()))  
  37.             return o1.getName().compareTo(o2.getName());  
  38.         else  
  39.             return o1.getClassnum().compareTo(o2.getClassnum());  
  40.           
  41.     }  
  42.       
  43. }  
  44. public class EX20_2 {  
  45.   
  46.     public static void main(String[] args) {  
  47.         TreeSet<Student> ts=new TreeSet<Student>(new studentcomparator());  
  48.         ts.add(new Student("1班","acy"));  
  49.         ts.add(new Student("2班","zdx"));  
  50.         ts.add(new Student("1班","qhj"));  
  51.         ts.add(new Student("3班","wdl"));  
  52.         ts.add(new Student("2班","ws"));  
  53.         //1.直接输出TreeSet  
  54.         System.out.println(ts);  
  55.         //2.通过迭代器输出  
  56.         Iterator<Student> it=ts.iterator();  
  57.         while(it.hasNext())  
  58.         {  
  59.             System.out.println(it.next());  
  60.         }  
  61.     }  
  62.   
  63. }  
输出:

[人名:acy,班级名称:1班
, 人名:qhj,班级名称:1班
, 人名:ws,班级名称:2班
, 人名:zdx,班级名称:2班
, 人名:wdl,班级名称:3班
]
人名:acy,班级名称:1班


人名:qhj,班级名称:1班


人名:ws,班级名称:2班


人名:zdx,班级名称:2班


人名:wdl,班级名称:3班

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值