引入緣由:java
Java中的對象,正常狀況下,只能進行比較:== 或!= ,不能使用 < 或 > ,可是在開發時須要用到比較對象的大小ide
1.像String 、包裝類等實現了Comparable接口,重寫了compareTo()方法,給出了比較兩個對象大小的方法this
2.像String 、包裝類等重寫了compareTo()方法後,默認執行了從小到大的排序spa
3.重寫compareTo()的規則:對象
若是當前對象this大於形參對象obj,則返回正整數,若是當前對象this小於形參對象obj,則返回負整數。若是當前對象this等於形參對象obj,則返回零。blog
4.對於自定義類來講,若是須要排序,咱們能夠讓自定義類實現Comparable接口,重寫compareTo()方法, 在compareTo()方法中指明如何排序排序
String類的天然排序實現舉例:接口
@Test public void test1(){ String[] arr=new String[] {"GG","MM","AA","CC","DD","KK"}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); //[AA, CC, DD, GG, KK, MM] }
自定義類的天然排序舉例: 開發
@Test public void test2(){ Goods[] arr=new Goods[4]; arr[0] = new Goods("lenovoMouse",34); arr[1] = new Goods("dellMouse",66); arr[2] = new Goods("xiaomiMouse",50); arr[3] = new Goods("hahaMouse",66); Arrays.sort(arr); System.out.println(Arrays.toString(arr)); /* 運行結果: [Goods{name='lenovoMouse', price=34.0}, Goods{name='xiaomiMouse', price=50.0}, Goods{name='dellMouse', price=66.0}, Goods{name='hahaMouse', price=66.0}] */ }
Goods商品類的實現代碼:字符串
public class Goods implements Comparable { private String name; private double price; public Goods() { } public Goods(String name, double price) { this.name = name; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Goods{" + "name='" + name + '\'' + ", price=" + price + '}'; } //指明商品比較大小的方式,按照價格從低到高排序,若是出現價格相同的,再按照產品名稱從低到高排序 @Override public int compareTo(Object o) { if(o instanceof Goods ){ Goods goods =(Goods)o; if (this.price > goods.price) { return 1; }else if(this.price < goods.price){ return -1; }else //return 0; return this.name.compareTo(goods.name); } throw new RuntimeException("傳入的數據類型不一致"); } }
2.1引入緣由:當元素的類型沒有實現java.lang.Comparable藉口而又不方便修改代碼,或者實現了java.lang.Comparable接口的排序規則不適合當前的操做能夠考慮使用Comparator的對象來實現排序。
2.2重寫compare(Object o1,Object o2)方法,比較o1和o2的大小,若是方法返回正整數,則表示o1大於o2,若是返回0,表示兩者相等,若是返回負整數,表示o1小於o2.
舉例一:
@Test public void test1(){ String[] arr=new String[] {"GG","MM","AA","CC","DD","KK"}; Arrays.sort(arr, new Comparator() { //按照字符串從大到小的順序排序 @Override public int compare(Object o1, Object o2) { if(o1 instanceof String && o2 instanceof String ){ String s1=(String)o1; String s2=(String)o2; return -s1.compareTo(s2); } //return 0; throw new RuntimeException("輸入的數據類型不一致"); } }); System.out.println(Arrays.toString(arr)); //[MM, KK, GG, DD, CC, AA] }
舉例二:
@Test public void test2(){ Goods[] arr=new Goods[5]; arr[0] = new Goods("lenovoMouse",34); arr[1] = new Goods("dellMouse",66); arr[2] = new Goods("xiaomiMouse",50); arr[3] = new Goods("hahaMouse",66); arr[4] = new Goods("hahaMouse",166); Arrays.sort(arr, new Comparator() { //指明商品比較大小的方式,按照產品名稱從低到高排序,再按照價格從高到低排序 @Override public int compare(Object o1, Object o2) { if(o1 instanceof Goods && o2 instanceof Goods){ Goods g1=(Goods)o1; Goods g2=(Goods)o2; if(g1.getName().equals(g2.getName())){ return -Double.compare(g1.getPrice(),g2.getPrice()); }else { return g1.getName().compareTo(g2.getName()); } } throw new RuntimeException("輸入的數據類型不一致"); } }); System.out.println(Arrays.toString(arr)); /* 運行結果: [Goods{name='dellMouse', price=66.0}, Goods{name='hahaMouse', price=166.0}, Goods{name='hahaMouse', price=66.0}, Goods{name='lenovoMouse', price=34.0}, Goods{name='xiaomiMouse', price=50.0}] */ }
2.3Comparable接口與Comparator接口的使用的對比:
Comparable接口的方式一旦指定,保證Comparable接口實現類的對象在任何位置均可以比較大小。
Comparator接口屬於臨時性的比較。