重载equals方法就一定要要重载hashCode方法

在进行对象之间的比较的时候,一般都会override equals方法,而hashCode的override很多时候都会被忽略。hashCode方法一般都被认为是用来判断两个对象是不是同一个引用的,它返回一串散列码,这个散列码的主要用途是进行Hash存储时,作为键用的,加快查找对象的速度。一般都会以31 29这样的素数作为基数,主要目的是为了减少在一个大的集合中键值的重复,导致冲突的情况发生,以提高性能。
有关深入HashCode方法详见:http://blog.csdn.net/axman/archive/2004/11/26/195159.aspx

下面有个Bean,在只重载了equals方法后,执行插入Set后的个数和重载了equals与hashCode方法两个方法后的Set集合中的对象个数:
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class ListSet
{
public static void main(String... a)
{
Set<UserBean> set = new HashSet<UserBean>();
Date date = new Date();
UserBean ub;
for (int i = 0; i < 20; i++)
{
ub = new UserBean();
ub.setUsername("javaeye");
ub.setPassword("javaeye");
ub.setSex(i % 2);
ub.setBirthDay(date);
set.add(ub);
}
System.out.println("Set中的UserBean对象个数:" + set.size());
}
}

class UserBean
{
private String username;
private String password;
private int sex;
private Date birthDay;

@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
final UserBean other = (UserBean) obj;
if (birthDay == null)
{
if (other.birthDay != null) return false;
}
else if (!birthDay.equals(other.birthDay)) return false;
if (password == null)
{
if (other.password != null) return false;
}
else if (!password.equals(other.password)) return false;
if (sex != other.sex) return false;
if (username == null)
{
if (other.username != null) return false;
}
else if (!username.equals(other.username)) return false;
return true;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public int getSex()
{
return sex;
}
public void setSex(int sex)
{
this.sex = sex;
}
public Date getBirthDay()
{
return birthDay;
}
public void setBirthDay(Date birthDay)
{
this.birthDay = birthDay;
}
}

执行结果是:Set中的UserBean对象个数:20
而加了hashCode方法后:
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class ListSet
{
public static void main(String... a)
{
Set<UserBean> set = new HashSet<UserBean>();
Date date = new Date();
UserBean ub;
for (int i = 0; i < 20; i++)
{
ub = new UserBean();
ub.setUsername("javaeye");
ub.setPassword("javaeye");
ub.setSex(i % 2);
ub.setBirthDay(date);
set.add(ub);
}
System.out.println("Set中的UserBean对象个数:" + set.size());
}
}

class UserBean
{
private String username;
private String password;
private int sex;
private Date birthDay;

@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((birthDay == null) ? 0 : birthDay.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
result = prime * result + sex;
result = prime * result + ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
final UserBean other = (UserBean) obj;
if (birthDay == null)
{
if (other.birthDay != null) return false;
}
else if (!birthDay.equals(other.birthDay)) return false;
if (password == null)
{
if (other.password != null) return false;
}
else if (!password.equals(other.password)) return false;
if (sex != other.sex) return false;
if (username == null)
{
if (other.username != null) return false;
}
else if (!username.equals(other.username)) return false;
return true;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public int getSex()
{
return sex;
}
public void setSex(int sex)
{
this.sex = sex;
}
public Date getBirthDay()
{
return birthDay;
}
public void setBirthDay(Date birthDay)
{
this.birthDay = birthDay;
}
}

执行结果是:Set中的UserBean对象个数:2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值