重写equals()与hashCode()方法。当两个对象因为某种逻辑比较的时候,调用对象的比较方法。在散列中,以hashCode对对象进行排列等情况下都要重写对象的这两个方法:

以下Copy网上比较全面的例子

 

  1. public class Unit { 
  2.     private short ashort; 
  3.     private char achar; 
  4.     private byte abyte; 
  5.     private boolean abool; 
  6.     private long along; 
  7.     private float afloat; 
  8.     private double adouble; 
  9.     private Unit aObject; 
  10.     private int[] ints; 
  11.     private Unit[] units; 
  12.   
  13.     public boolean equals(Object o) { 
  14.        if (!(o instanceof Unit)) 
  15.            return false
  16.        Unit unit = (Unit) o; 
  17.        return unit.ashort == ashort 
  18.               && unit.achar == achar 
  19.               && unit.abyte == abyte 
  20.               && unit.abool == abool 
  21.               && unit.along == along 
  22.               && Float.floatToIntBits(unit.afloat) == Float 
  23.                      .floatToIntBits(afloat) 
  24.               && Double.doubleToLongBits(unit.adouble) == Double 
  25.                      .doubleToLongBits(adouble) 
  26.               && unit.aObject.equals(aObject) 
  27. && equalsInts(unit.ints) 
  28.               && equalsUnits(unit.units); 
  29.     } 
  30.   
  31.     private boolean equalsInts(int[] aints) { 
  32.        return Arrays.equals(ints, aints); 
  33.     } 
  34.   
  35.     private boolean equalsUnits(Unit[] aUnits) { 
  36.        return Arrays.equals(units, aUnits); 
  37.     } 
  38.   
  39.     public int hashCode() { 
  40.        int result = 17
  41.        result = 37 * result + (int) ashort; 
  42.        result = 37 * result + (int) achar; 
  43.        result = 37 * result + (int) abyte; 
  44.        result = 37 * result + (abool ? 0 : 1); 
  45.        result = 37 * result + (int) (along ^ (along >>> 32)); 
  46.        result = 37 * result + Float.floatToIntBits(afloat); 
  47.        long tolong = Double.doubleToLongBits(adouble); 
  48.        result = 37 * result + (int) (tolong ^ (tolong >>> 32)); 
  49.        result = 37 * result + aObject.hashCode(); 
  50.        result = 37 * result + intsHashCode(ints); 
  51.        result = 37 * result + unitsHashCode(units); 
  52.        return result; 
  53.     } 
  54.   
  55.     private int intsHashCode(int[] aints) { 
  56.        int result = 17
  57.        for (int i = 0; i < aints.length; i++) 
  58.            result = 37 * result + aints[i]; 
  59.        return result; 
  60.     } 
  61.   
  62.     private int unitsHashCode(Unit[] aUnits) { 
  63.        int result = 17
  64.        for (int i = 0; i < aUnits.length; i++) 
  65.            result = 37 * result + aUnits[i].hashCode(); 
  66.        return result; 
  67.     } 

 如当散列中的对象Key木有复写hashCode方法,很容易找不到该Key所对应的值,如:

 

  1. public class Factory { 
  2.      
  3.     private String product; 
  4.      
  5.     public void setProduct(String product) { 
  6.         this.product = product; 
  7.     } 
  8.      
  9.     public String getProduct() { 
  10.         return product; 
  11.     } 
  12.      
  13.     public Factory(String produc) { 
  14.         this.product=produc; 
  15.     } 
  16.      
  17.     @Override 
  18.     public boolean equals(Object obj) { 
  19.         if (obj==this) { 
  20.             return true; 
  21.         } 
  22.         if (!(obj instanceof Factory)) { 
  23.             return false; 
  24.         } 
  25.         Factory factory=(Factory) obj; 
  26.         return this.product==factory.product; 
  27.     } 
  28.      
  29.     @Override 
  30.     public int hashCode() { 
  31.         return super.hashCode(); 
  32.     } 
  33.  
  34.  
  35.  
  36.         HashMap<Factory,String> map=new HashMap<Factory, String>(); 
  37.         map.put(new Factory("产品1"), "product1"); 
  38.         System.out.println(map.get(new Factory("产品1"))); //返回null

这是因为散列中第一次put进去的Key的hashCode与获取的key的hashCode不是同一个int值。