欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

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

发布时间:2025/7/14 编程问答 89 豆豆
生活随笔 收集整理的这篇文章主要介绍了 think in java interview-高级开发人员面试宝典(三) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

comparable接口与comparator

两种比较接口分析


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


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


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


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

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

来看样例:

PersonBean类

[java]view plaincopy
  • <span style="font-size:12px;">publicclass PersonBean implements Comparable<PersonBean> {  

  • public PersonBean(int age, String name) {  

  • this.age = age;  

  • this.name = name;  

  •    }  

  • int age = 0;  

  •    String name = "";  

  • publicint getAge() {  

  • return age;  

  •    }  

  • publicvoid setAge(int age) {  

  • this.age = age;  

  •    }  

  • public String getName() {  

  • return name;  

  •    }  

  • publicvoid setName(String name) {  

  • this.name = name;  

  •    }  

  • publicboolean equals(Object o) {  

  • if (!(o instanceof PersonBean)) {  

  • returnfalse;  

  •        }  

  •        PersonBean p = (PersonBean) o;  

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

  •    }  

  • publicint hashCode() {  

  • int result = 17;  

  •        result = 31 * result + age;  

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

  • return result;  

  •    }  

  • public String toString() {  

  • return (age + "{" + name + "}");  

  •    }  

  • publicint compareTo(PersonBean person) {  

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

  • if (cop != 0)  

  • return cop;  

  • else

  • return name.compareTo(person.name);  

  •    }  

  • }  

  • </span>  

  • AlphDesc类


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

  • publicclass AlphDesc implements Comparator<PersonBean> {  

  • publicint compare(PersonBean personA, PersonBean personB) {  

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

  • if (cop != 0)  

  • return cop;  

  • else

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

  •    }  

  • }  

  • </span>  


  • TestComparable类


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

  • publicclass TestComparable {  

  • /**

  •     * @param args

  •     */

  • publicvoid compare() {  

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

  • new PersonBean(20, "Jeff"),  

  • new PersonBean(30, "Mary"),  

  • new PersonBean(20, "Ada"),  

  • new PersonBean(40, "Walton"),  

  • new PersonBean(61, "Peter"),  

  • new PersonBean(20, "Bush") };  

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

  •        AlphDesc desc = new AlphDesc();  

  •        Arrays.sort(p,desc);  

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

  •    }  

  • publicstaticvoid main(String[] args) {  

  •        TestComparable tc = new TestComparable();  

  •        tc.compare();  

  •    }  

  • }</span>  




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


    转载于:https://blog.51cto.com/longx/1351868

    总结

    以上是生活随笔为你收集整理的think in java interview-高级开发人员面试宝典(三)的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。