Java面向对象_常用类库api——对象比较器

概念:对两个或多个数据项进行比较,以确定他们是否相等,或确定他们之间的大小关系及排列顺序成为比较。

1.Comparable接口:

   此接口强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序,类的compareTo方法被称为它的自然比较方法。

 1 public class ComparableDemo {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         Cat[] cats={new Cat("tom",4),new Cat("jack",3),new Cat("xiaohua",5)};
 9         Arrays.sort(cats);
10         for (Cat cat : cats) {
11             System.out.println(cat);
12         }
13     }
14 
15 }
 1 public class Cat implements Comparable<Cat> {
 2     private String name;
 3     private int age;
 4     
 5     public Cat() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9     public Cat(String name, int age) {
10         super();
11         this.name = name;
12         this.age = age;
13     }
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     public int getAge() {
21         return age;
22     }
23     public void setAge(int age) {
24         this.age = age;
25     }
26     @Override
27     public String toString() {
28         return "Cat [name=" + name + ", age=" + age + "]";
29     }
30     //通过此方法实现对象比较
31     @Override
32     public int compareTo(Cat o) {
33         // TODO Auto-generated method stub
34         if(this.age <o.age ){//从小到大排序
35             return -1;
36         }else if(this.age >o.age ){
37             return 1;
38         }else{
39         return 0;
40     }
41     }
42     
43 }

 

2.Comparator接口:(不改变类,再写一个比较类)

   强行对某个对象collection进行整体排序的比较。

 1 public class ComparableDemo {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         Dog[] dogs={new Dog("tom",4),new Dog("jack",3),new Dog("xiaohua",5)};
 9         Arrays.sort(dogs, new DogComparator());
10         for (Dog dog : dogs) {
11             System.out.println(dog);
12         }
13     }
14 
15 }
 1 public class Dog {
 2     private String name;
 3     private int age;
 4     public Dog() {
 5         super();
 6         // TODO Auto-generated constructor stub
 7     }
 8     public Dog(String name, int age) {
 9         super();
10         this.name = name;
11         this.age = age;
12     }
13     public String getName() {
14         return name;
15     }
16     public void setName(String name) {
17         this.name = name;
18     }
19     public int getAge() {
20         return age;
21     }
22     public void setAge(int age) {
23         this.age = age;
24     }
25     @Override
26     public String toString() {
27         return "Dog [name=" + name + ", age=" + age + "]";
28     }
29 }
 1 import java.util.Comparator;
 2 
 3 public class DogComparator implements Comparator<Dog>{
 4 
 5     @Override
 6     public int compare(Dog o1, Dog o2) {
 7         // TODO Auto-generated method stub
 8         if(o1.getAge()<o2.getAge()){
 9             return -1;
10         }else if(o1.getAge()>o2.getAge()){
11             return 1;
12         }else{
13         return 0;
14     }
15     }
16         
17 }

 

posted on 2015-12-30 15:14  深海溺心  阅读(270)  评论(0编辑  收藏  举报

导航