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()为什么要这么写?
}
}
* @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()为什么要这么写?
}
}
hashCode在HashSet、HashMap等的对象中都必须定义,可以按自己的需要
首先它使用了两个对象的hashcode,如果他们都是分配均匀的,那么新运算得到的hashcode也会均匀。
另外它使用了位运算,使得新的hashcode不会与他们两个中的一个相同。