日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/7/14 83 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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-高级开发人员面试宝典(三)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。