首页 > 代码库 > 对hashcode()的解读转自http://www.th7.cn/Program/java/201404/189079.shtml

对hashcode()的解读转自http://www.th7.cn/Program/java/201404/189079.shtml

11. public class Person {
12. private String name, comment;
13. private int age;
14. public Person(String n, int a, String c) {
15. name = n; age = a; comment = c;
16. }
17. public boolean equals(Object o) {
18. if (! (o instanceof Person)) return false;
19, Person p = (Person)o;
20. return age == p.age && name.equals(p.name);
21. }
22. }

What is the appropriate definition of  the hashCode method in class Person?
A. return super.hashCode();
B. return name.hashCode() + age * 7;
C. return name.hashCode() + comment.hashCode() / 2;
D. return name.hashCode() + comment.hashCode() / 2 - age * 3;

Answer: B

 

hashCode

public int hashCode()

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.

返回一个对象的hash code值。这个方法被用在hash tables中,例如HashMap

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • 在一个执行的java程序里面,同一个对象多次调用hashcode,那么这个方法就会固定地返回一个相同的整数值,前提是将对象进行 equals 比较时所用的信息没有被修改。这个整数值在不同的java程序执行中不一定相同。
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • 如果根据 equals(Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 hashCode 方法都必须生成相同的整数结果。
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
  • 如果根据 equals(java.lang.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 JavaTM programming language.)

实际上,由 Object 类定义的 hashCode 方法确实会针对不同的对象返回不同的整数。一般是通过将该对象的内部地址转换成一个整数来实现的

Returns:

a hash code value for this object.

See Also:

equals(java.lang.Object), System.identityHashCode(java.lang.Object)

题目分析:

很明显hashcode跟equals有关,这里重写了equals,解读这个equals()发现是根据年龄和名字来判断是否相同的。只要名字和年龄相同了,hashcode返回整数就相同。

首先是C和D:有个comment在里面,不符合上equals()的条件,不行。

然后A、B:根据“由 Object 类定义的 hashCode 方法确实会针对不同的对象返回不同的整数。”,A对不同对象产生相同的hashCode返回值。所以A错。

答案B

对hashcode()的解读转自http://www.th7.cn/Program/java/201404/189079.shtml