a) 重寫 hashCode() 和 equals() 兩個方法html
b) 具體舉例:數組
1 class Stu{ 2 private String name = null; 3 private int age = 0; 4 5 public Stu(String name, int age){ 6 this.name = name; 7 this.age = age; 8 } 9 //覆寫Object中的equals方法 10 public boolean equals(Object obj){ 11 if(this == obj){ 12 return true; 13 } 14 if(!(obj instanceof Stu)){ 15 return false; 16 } 17 Stu stu = (Stu)obj; 18 if((stu.age == this.age) && this.name.equals(stu.name)){ 19 return true; 20 } 21 return false; 22 } 23 //覆寫Object中的hashCode方法 24 public int hashCode(){ 25 return this.name.hashCode() * this.age; 26 } 27 }
實現Comparable接口並重寫compareTo()方法, 在compareTo()方法中指明如何排序。ide
1 public class Goods implements Comparable { 2 private String name; 3 private double price; 4 5 public Goods() { 6 } 7 8 public Goods(String name, double price) { 9 this.name = name; 10 this.price = price; 11 } 12 13 public String getName() { 14 return name; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 public double getPrice() { 22 return price; 23 } 24 25 public void setPrice(double price) { 26 this.price = price; 27 } 28 29 @Override 30 public String toString() { 31 return "Goods{" + 32 "name='" + name + '\'' + 33 ", price=" + price + 34 '}'; 35 } 36 37 //指明商品比較大小的方式,按照價格從低到高排序,若是出現價格相同的,再按照產品名稱從低到高排序 38 @Override 39 public int compareTo(Object o) { 40 if(o instanceof Goods ){ 41 Goods goods =(Goods)o; 42 if (this.price > goods.price) { 43 return 1; 44 }else if(this.price < goods.price){ 45 return -1; 46 }else 47 //return 0; 48 return this.name.compareTo(goods.name); 49 } 50 throw new RuntimeException("傳入的數據類型不一致"); 51 } 52 }
傳入Comparator並在實例對象中重寫compare(Object o1,Object o2)方法,比較o1和o2的大小,若是方法返回正整數,則表示o1大於o2,若是返回0,表示兩者相等,若是返回負整數,表示o1小於o2.工具
1 // 運用集合工具類 Collections.sort() 2 @Test 3 public void test1(){ 4 Map<Integer,String> map = new TreeMap <>(); 5 map.put(5, "a"); 6 map.put(3, "c"); 7 map.put(4, "b"); 8 map.put(2, "d"); 9 map.put(1, "e"); 10 List<Entry<Integer,String>> list = new ArrayList<>(map.entrySet()); 11 Collections.sort(list, new Comparator<Entry<Integer,String>>() { 12 @Override 13 public int compare(Entry<Integer,String> o1, Entry<Integer,String> o2) { 14 // TODO Auto-generated method stub 15 return o1.getValue().compareTo(o2.getValue()); 16 } 17 }); 18 for(Entry<Integer,String> aEntry : list) { 19 System.out.println(aEntry.getKey()+":"+aEntry.getValue()); 20 } 21 22 /* 23 運行結果: 24 5:a 25 4:b 26 3:c 27 2:d 28 1:e 29 */ 30 } 31 32 // 運用數組工具類 Arrays.sort() 33 @Test 34 public void test2(){ 35 Goods[] arr=new Goods[5]; 36 arr[0] = new Goods("lenovoMouse",34); 37 arr[1] = new Goods("dellMouse",66); 38 arr[2] = new Goods("xiaomiMouse",50); 39 arr[3] = new Goods("hahaMouse",66); 40 arr[4] = new Goods("hahaMouse",166); 41 42 Arrays.sort(arr, new Comparator() { 43 //指明商品比較大小的方式,按照產品名稱從低到高排序,再按照價格從高到低排序 44 @Override 45 public int compare(Object o1, Object o2) { 46 if(o1 instanceof Goods && o2 instanceof Goods){ 47 48 Goods g1=(Goods)o1; 49 Goods g2=(Goods)o2; 50 if(g1.getName().equals(g2.getName())){ 51 return -Double.compare(g1.getPrice(),g2.getPrice()); 52 }else { 53 return g1.getName().compareTo(g2.getName()); 54 } 55 } 56 throw new RuntimeException("輸入的數據類型不一致"); 57 } 58 }); 59 60 System.out.println(Arrays.toString(arr)); 61 62 /* 63 運行結果: 64 [Goods{name='dellMouse', price=66.0}, Goods{name='hahaMouse', price=166.0}, 65 Goods{name='hahaMouse', price=66.0}, Goods{name='lenovoMouse', price=34.0}, 66 Goods{name='xiaomiMouse', price=50.0}] 67 */ 68 }
Comparable接口的方式一旦指定,保證Comparable接口實現類的對象在任何位置均可以比較大小。post
Comparator接口屬於臨時性的比較。this
引用資料:url
1.如何判斷兩個對象是否徹底相等spa
2.Java中比較對象大小的兩種實現方式code