jdk文檔是這樣介紹的數據結構
1. public interface Comparable<T>:此接口強行對實現它的每一個類的對象進行總體排序,也就是所謂的天然排序。它的compareTo方法被稱爲天然比較方法。ide
Collections.sort(List<Clazz>)這個Clazz實現了Comparable接口函數
2. public interface Comparator<T>:強行對某個對象collection進行總體排序的比較函數。能夠將Comparator傳遞給sort方法,也能夠使用它來控制某些數據結構的順序,或者爲那些沒有天然順序的對象collection提供排序,重寫compare方法。ui
Collections.sort(List<Clazz>,clazz)clazz實現了Comparator接口spa
我以爲使用Comparator 比使用Comparable更靈活一點吧。對象
第一種方式實現Comparable排序
@Override public int compareTo(ConsumInfo o) { if(price < o.price){ return -1; } if (price > o.price){ return 1; } if(price == o.price){ if (uid < o.uid){ return -1; } if (uid > o.uid){ return 1; } } return 0; }
test類接口
public static void main(String[] args) { ConsumInfo consumInfo1 = new ConsumInfo(100, "consumInfo1", 400.0,new Date()); ConsumInfo consumInfo2 = new ConsumInfo(200, "consumInfo1", 200.0,new Date()); ConsumInfo consumInfo3 = new ConsumInfo(300, "consumInfo1", 100.0,new Date()); ConsumInfo consumInfo4 = new ConsumInfo(400, "consumInfo1", 700.0,new Date()); ConsumInfo consumInfo5 = new ConsumInfo(500, "consumInfo1", 800.0,new Date()); ConsumInfo consumInfo6 = new ConsumInfo(600, "consumInfo1", 300.0,new Date()); ConsumInfo consumInfo7 = new ConsumInfo(700, "consumInfo1", 900.0,new Date()); ConsumInfo consumInfo8 = new ConsumInfo(800, "consumInfo1", 400.0,new Date()); List<ConsumInfo> list = new ArrayList<ConsumInfo>(); list.add(consumInfo1); list.add(consumInfo2); list.add(consumInfo3); list.add(consumInfo4); list.add(consumInfo5); list.add(consumInfo6); list.add(consumInfo7); list.add(consumInfo8); System.out.println("排序前:"); for (ConsumInfo consumInfo : list){ System.out.println(consumInfo); } Collections.sort(list); System.out.println("排序後:"); for (Comparable comparable : list){ System.out.println(comparable); } }
結果是:文檔
排序前:
ConsumInfo{uid=100, name='consumInfo1', price=400.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=200, name='consumInfo1', price=200.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=300, name='consumInfo1', price=100.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=400, name='consumInfo1', price=700.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=500, name='consumInfo1', price=800.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=600, name='consumInfo1', price=300.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=700, name='consumInfo1', price=900.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=800, name='consumInfo1', price=400.0, datetime=Sun Aug 26 22:24:23 CST 2018}
排序後:
ConsumInfo{uid=300, name='consumInfo1', price=100.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=200, name='consumInfo1', price=200.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=600, name='consumInfo1', price=300.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=100, name='consumInfo1', price=400.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=800, name='consumInfo1', price=400.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=400, name='consumInfo1', price=700.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=500, name='consumInfo1', price=800.0, datetime=Sun Aug 26 22:24:23 CST 2018}
ConsumInfo{uid=700, name='consumInfo1', price=900.0, datetime=Sun Aug 26 22:24:23 CST 2018}get
第二種方式,實現Comparator
public class ConsumIfoComapratorClass implements Comparator<ConsumIfoComparator> { @Override public int compare(ConsumIfoComparator o1, ConsumIfoComparator o2) { if (o1.getPrice() < o2.getPrice()){ return -1; } if (o1.getPrice() > o2.getPrice()){ return 1; } if(o1.getPrice() == o2.getPrice()){ if(o1.getUid() < o2.getUid()){ return -1; } if(o1.getUid() > o2.getUid()){ return 1; } } return 0; } }
test類
實體類ConsumIfoComparator 沒有實現別的排序接口
public static void main(String[] args) { ConsumIfoComparator consumInfo1 = new ConsumIfoComparator(100, "consumInfo1", 400.0,new Date()); ConsumIfoComparator consumInfo2 = new ConsumIfoComparator(200, "consumInfo1", 200.0,new Date()); ConsumIfoComparator consumInfo3 = new ConsumIfoComparator(300, "consumInfo1", 100.0,new Date()); ConsumIfoComparator consumInfo4 = new ConsumIfoComparator(400, "consumInfo1", 700.0,new Date()); ConsumIfoComparator consumInfo5 = new ConsumIfoComparator(500, "consumInfo1", 800.0,new Date()); ConsumIfoComparator consumInfo6 = new ConsumIfoComparator(600, "consumInfo1", 300.0,new Date()); ConsumIfoComparator consumInfo7 = new ConsumIfoComparator(700, "consumInfo1", 900.0,new Date()); ConsumIfoComparator consumInfo8 = new ConsumIfoComparator(800, "consumInfo1", 400.0,new Date()); List<ConsumIfoComparator> list = new ArrayList<ConsumIfoComparator>(); list.add(consumInfo1); list.add(consumInfo2); list.add(consumInfo3); list.add(consumInfo4); list.add(consumInfo5); list.add(consumInfo6); list.add(consumInfo7); list.add(consumInfo8); System.out.println("排序前:"); for (ConsumIfoComparator consumIfoComparator : list){ System.out.println(consumIfoComparator); } ConsumIfoComapratorClass consumIfoComapratorClass = new ConsumIfoComapratorClass();//比較器 Collections.sort(list,consumIfoComapratorClass); System.out.println("排序後:"); for (ConsumIfoComparator consumIfoComparator : list){ System.out.println(consumIfoComparator); } }
結果是:
排序前:
ConsumIfoComparator{uid=100, name='consumInfo1', price=400.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=200, name='consumInfo1', price=200.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=300, name='consumInfo1', price=100.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=400, name='consumInfo1', price=700.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=500, name='consumInfo1', price=800.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=600, name='consumInfo1', price=300.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=700, name='consumInfo1', price=900.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=800, name='consumInfo1', price=400.0, datetime=Sun Aug 26 22:26:38 CST 2018}
排序後:
ConsumIfoComparator{uid=300, name='consumInfo1', price=100.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=200, name='consumInfo1', price=200.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=600, name='consumInfo1', price=300.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=100, name='consumInfo1', price=400.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=800, name='consumInfo1', price=400.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=400, name='consumInfo1', price=700.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=500, name='consumInfo1', price=800.0, datetime=Sun Aug 26 22:26:38 CST 2018}
ConsumIfoComparator{uid=700, name='consumInfo1', price=900.0, datetime=Sun Aug 26 22:26:38 CST 2018}
總結一下:實現Comparable接口的類須要重寫CompareTo()方法,實現了這個接口的類就能夠實現排序了。也就是說該類的對象是一個可比較的對象,可是也須要修改源碼。Collections.sort(list);
而實現了Comparator 接口的類須要重寫compare()方法,實現這個接口不須要修改源碼而是須要寫一個比較器傳入須要比較的對象,經過編寫着本身實現比較的規則,這就能夠本身定義更加複雜的規則了。Collections.sort(list,consumIfoComapratorClass);