public class RecordId implements Serializable {     private User user;     private Goods goods;     /** 
     * @return Returns the goods. 
     */ 
    public Goods getGoods() { 
        return goods; 
    }     /** 
     * @param goods 
     *            The goods to set. 
     */ 
    public void setGoods(Goods goods) { 
        this.goods = goods; 
    }     /** 
     * @return Returns the user. 
     */ 
    public User getUser() { 
        return user; 
    }     /** 
     * @param user 
     *            The user to set. 
     */ 
    public void setUser(User user) { 
        this.user = user; 
    }     public boolean equals(Object obj) { 
        return (obj instanceof RecordId) 
                && (this.getUser().equals(((RecordId) obj).getUser())) 
                && (this.getGoods().equals(((RecordId) obj).getGoods())); 
    }     public int hashCode() { 
        return this.getUser().hashCode() ^ this.getGoods().hashCode();// 那位高人解释这个hashCode()为什么要这么写?
    } 
}

解决方案 »

  1.   

    正如楼上所说的,是自己定义的,
    hashCode在HashSet、HashMap等的对象中都必须定义,可以按自己的需要
      

  2.   

    好的hashcode函数 的作用是尽量使得得到的数字分配均匀,但并不要求得到的数字唯一。所以,任何的hashcode函数都是合语法的。这个例子的hashcode的思路是使用两个对象的自带的hashcode进行位运算,得到新的hashcode。这是比较简单且有效的方法:
    首先它使用了两个对象的hashcode,如果他们都是分配均匀的,那么新运算得到的hashcode也会均匀。
    另外它使用了位运算,使得新的hashcode不会与他们两个中的一个相同。