think in java interview-高级开发人员面试宝典(三)

comparable接口与comparator

两种比较接口分析


前者应该比较固定,和一个具体类相绑定,而后者比较灵活,它可以被用于各个需要比较功能的类使用。


一个类实现了 Camparable 接口表明这个类的对象之间是可以相互比较的。如果用数学语言描述的话就是这个类的对象组成的集合中存在一个全序。这样,这个类对象组成的集合就可以使用 Sort 方法排序了。


而 Comparator 的作用有两个:
1. 如果类的设计师没有考虑到 Compare 的问题而没有实现 Comparable 接口,可以通过 Comparator 来实现比较算法进行排序;
2. 为了使用不同的排序标准做准备,比如:升序、降序或其他什么序。


在 “集合框架” 中有两种比较接口: Comparable 接口和 Comparator 接口。

Comparable 是通用的接口,用户可以实现它来完成自己特定的比较,而 Comparator 可以看成一种算法的实现,在需要容器集合实现比较功能的时候,来指定这个比较器,这可以看成一种设计模式,将算法和数据分离。

来看样例:

PersonBean类

[java] view plain copy
  1. <span style="font-size:12px;">publicclass PersonBean implements Comparable<PersonBean> {  

  2. public PersonBean(int age, String name) {  

  3. this.age = age;  

  4. this.name = name;  

  5.    }  

  6. int age = 0;  

  7.    String name = "";  

  8. publicint getAge() {  

  9. return age;  

  10.    }  

  11. publicvoid setAge(int age) {  

  12. this.age = age;  

  13.    }  

  14. public String getName() {  

  15. return name;  

  16.    }  

  17. publicvoid setName(String name) {  

  18. this.name = name;  

  19.    }  

  20. publicboolean equals(Object o) {  

  21. if (!(o instanceof PersonBean)) {  

  22. returnfalse;  

  23.        }  

  24.        PersonBean p = (PersonBean) o;  

  25. return (age == p.age) && (name.equals(p.name));  

  26.    }  

  27. publicint hashCode() {  

  28. int result = 17;  

  29.        result = 31 * result + age;  

  30.        result = 31 * result + name.hashCode();  

  31. return result;  

  32.    }  

  33. public String toString() {  

  34. return (age + "{" + name + "}");  

  35.    }  

  36. publicint compareTo(PersonBean person) {  

  37. int cop = age - person.getAge();  

  38. if (cop != 0)  

  39. return cop;  

  40. else

  41. return name.compareTo(person.name);  

  42.    }  

  43. }  

  44. </span>  

AlphDesc类


[java] view plain copy
  1. <span style="font-size:12px;">import java.util.Comparator;  

  2. publicclass AlphDesc implements Comparator<PersonBean> {  

  3. publicint compare(PersonBean personA, PersonBean personB) {  

  4. int cop = personA.age - personB.age;  

  5. if (cop != 0)  

  6. return cop;  

  7. else

  8. return personB.getName().compareTo(personA.getName());  

  9.    }  

  10. }  

  11. </span>  


TestComparable类


[java] view plain copy
  1. <span style="font-size:12px;">import java.util.*;  

  2. publicclass TestComparable {  

  3. /**

  4.     * @param args

  5.     */

  6. publicvoid compare() {  

  7.        PersonBean[] p = { new PersonBean(20, "Tom"),  

  8. new PersonBean(20, "Jeff"),  

  9. new PersonBean(30, "Mary"),  

  10. new PersonBean(20, "Ada"),  

  11. new PersonBean(40, "Walton"),  

  12. new PersonBean(61, "Peter"),  

  13. new PersonBean(20, "Bush") };  

  14.        System.out.println("before sort:\n" + Arrays.toString(p));  

  15.        AlphDesc desc = new AlphDesc();  

  16.        Arrays.sort(p,desc);  

  17.        System.out.println("after sort:\n" + Arrays.toString(p));  

  18.    }  

  19. publicstaticvoid main(String[] args) {  

  20.        TestComparable tc = new TestComparable();  

  21.        tc.compare();  

  22.    }  

  23. }</span>  




每一篇不宜写得过长,下篇继续


你可能感兴趣的:(java,in,think)