equals() & hashCode()为什么有时需要重写

本文详细解释了hashCode方法的重要性及其实现原理,并探讨了在何种情况下需要重写此方法。同时,文章通过实例展示了hashCode与equals方法之间的联系,强调了在特定场景下两者都需要被重写的必要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

翻阅一些资料后:
[b]段落1:[/b]
为什么要重写hashCode()方法呢,主要是为了return 我们想要的hashCode值,这些值是整型。
首先看看原始的Object类中的hashCode():
public native int hashCode();

看看对这个方法的描述:
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java TM programming language.)

hashCode是用来区别对象和对象用的,hashCode是int 值, 它对应这个对象在内存中的地址。

public class Employee {	
private int id;

public Employee(int id){
this.id = id;
}

public static void main(String[] args){
Employee emp1 = new Employee(11);
Employee emp2 = new Employee(12);

System.out.println(emp1.hashCode());
System.out.println(emp2.hashCode());
}
}


输出结果是:
3526198
7699183

[b]段落2:[/b]
重写hashCode()后:
public class Employee {	
private int id;

public Employee(int id){
this.id = id;
}

public int hashCode(){
return id;
}

public static void main(String[] args){
Employee emp1 = new Employee(11);
Employee emp2 = new Employee(12);

System.out.println(emp1.hashCode());
System.out.println(emp2.hashCode());
}
}

输出:
11
12

[color=red]Q[/color]:什么时候我们需要重写hashCode()方法?
[color=green]A[/color]:需要用该对象的hashCode值来区分对象的时候。比如将这些对象作为elements放入HashSet中,或者将这些对象存放作HashMap、Hashtable的Key值的时候。 还有补充吗?
而且,在重写hashCode()方法的同时,必须重写equals()方法,为什么?请看下面例子

只重写hashCode()的情况:
public class Employee {	
private int id;

public Employee(int id){
this.id = id;
}

public int hashCode(){
return id;
}
}
public class EmpTester {
public static void main(String[] args){
Employee emp1 = new Employee(11);
Employee emp2 = new Employee(12);
Employee emp3 = new Employee(13);

HashSet<Employee> hs = new HashSet<Employee>();

hs.add(emp1);
hs.add(emp2);
hs.add(emp3);

System.out.println(hs.size());
System.out.println(hs.contains(new Employee(11)));
System.out.println(hs.remove(new Employee(12)));
System.out.println(hs.size());
}
}

尽管两个Employee对象的hashCode是相同的,但因为没有重写equals()方法,结果是……
[color=green]3
false
false
3[/color]


equals() & hashCode()都重写的情况:

public class Employee {
private int id;

public Employee(int id){
this.id = id;
}

public int hashCode(){
return id;
}

public boolean equals(Object obj){
if(this == obj)
return true;
if(obj instanceof Employee){
Employee emp = (Employee)obj;
if(emp.id == id){
return true;
}else
return false;
}else
return false;
}
}

public class EmpTester {
public static void main(String[] args){
Employee emp1 = new Employee(11);
Employee emp2 = new Employee(12);
Employee emp3 = new Employee(13);

HashSet<Employee> hs = new HashSet<Employee>();

hs.add(emp1);
hs.add(emp2);
hs.add(emp3);

System.out.println(hs.size());
System.out.println(hs.contains(new Employee(11)));
System.out.println(hs.remove(new Employee(12)));
System.out.println(hs.size());
}
}

结果是:
[color=green]3
true
true
2[/color]


差不多了
具体请参考:
[url]http://www.javaworld.com/community/?q=node/1006[/url]
[url]http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object.html?page=1[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值