翻阅一些资料后:
[b]段落1:[/b]
为什么要重写hashCode()方法呢,主要是为了return 我们想要的hashCode值,这些值是整型。
首先看看原始的Object类中的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 值, 它对应这个对象在内存中的地址。
输出结果是:
3526198
7699183
[b]段落2:[/b]
重写hashCode()后:
输出:
11
12
[color=red]Q[/color]:什么时候我们需要重写hashCode()方法?
[color=green]A[/color]:需要用该对象的hashCode值来区分对象的时候。比如将这些对象作为elements放入HashSet中,或者将这些对象存放作HashMap、Hashtable的Key值的时候。 还有补充吗?
而且,在重写hashCode()方法的同时,必须重写equals()方法,为什么?请看下面例子
只重写hashCode()的情况:
尽管两个Employee对象的hashCode是相同的,但因为没有重写equals()方法,结果是……
[color=green]3
false
false
3[/color]
equals() & hashCode()都重写的情况:
结果是:
[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]
[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]