hashCode() 方法
hashCode() method
我需要一些帮助,以便从理论上更好地理解 hashCode()
方法。我读过(强调我的):
When hashCode()
is called on two separate objects (which are equal according to the equals()
method) it returns the same hash code value. However, if it is called on two unequal objects, it will not necessarily return different integer values.
哪里会出现异常?
public int hashCode() {
return 27;
}
信不信由你,尽管这不是一种非常有效的工作方式,但这是 hashCode 的有效实现,因为它会遵守 equals 方法的约定。此实施将导致您所描述的情况。
hashCode 用于限制要比较的案例数。
例如,如果在一所高中,您正在寻找一名学生。你只知道这个学生的姓名、性别和年龄。您是要查看所有学生,还是只查看该年龄和性别的学生?
对于某些数据结构,hashCode 也是如此。在寻找一个项目时,它会首先对具有相同 hashCode 的项目进行 sub-list/collection,然后,它会在 sub-list/collection.
中搜索确切的项目
哈希码越具体,搜索效率就越高。
假设您有一个包含两个字符串字段的 class,并且其哈希码是通过将这两个字段的哈希码相加计算得出的。进一步假设您有一个 equals,它只检查 class 字段值是否相等。
class Test {
String a;
String b;
public Test
@Override
public int hashCode() {
return a.hashCode() + b.hashCode();
}
@Override
public boolean equals(Object o) { // simplified
Test other = (Test)o;
return a.equals(other.a) && b.equals(other.b);
}
}
让我们看看不相等的实例是否可以具有相同的哈希码
Test t1 = new Test("hello", "world");
Test t2 = new Test("world", "hello");
System.out.println(t1.equals(t2)); // false
System.out.println(t1.hashCode() == t2.hashCode()); // true
我们还在尊重hashCode
's contract吗?
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.
嗯,是的,因为它仅取决于 a
和 b
,并且我们正在使用他们的 hashCode
方法,我们可以假设该方法尊重合同。
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.
确实如此
Test t1 = new Test("hello", "world");
Test t2 = new Test("hello", "world");
System.out.println(t1.equals(t2)); // true
System.out.println(t1.hashCode() == t2.hashCode()); // true
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.
这就是我们最初试图展示的内容。这不是必需的。
我需要一些帮助,以便从理论上更好地理解 hashCode()
方法。我读过(强调我的):
When
hashCode()
is called on two separate objects (which are equal according to theequals()
method) it returns the same hash code value. However, if it is called on two unequal objects, it will not necessarily return different integer values.
哪里会出现异常?
public int hashCode() {
return 27;
}
信不信由你,尽管这不是一种非常有效的工作方式,但这是 hashCode 的有效实现,因为它会遵守 equals 方法的约定。此实施将导致您所描述的情况。
hashCode 用于限制要比较的案例数。 例如,如果在一所高中,您正在寻找一名学生。你只知道这个学生的姓名、性别和年龄。您是要查看所有学生,还是只查看该年龄和性别的学生?
对于某些数据结构,hashCode 也是如此。在寻找一个项目时,它会首先对具有相同 hashCode 的项目进行 sub-list/collection,然后,它会在 sub-list/collection.
中搜索确切的项目哈希码越具体,搜索效率就越高。
假设您有一个包含两个字符串字段的 class,并且其哈希码是通过将这两个字段的哈希码相加计算得出的。进一步假设您有一个 equals,它只检查 class 字段值是否相等。
class Test {
String a;
String b;
public Test
@Override
public int hashCode() {
return a.hashCode() + b.hashCode();
}
@Override
public boolean equals(Object o) { // simplified
Test other = (Test)o;
return a.equals(other.a) && b.equals(other.b);
}
}
让我们看看不相等的实例是否可以具有相同的哈希码
Test t1 = new Test("hello", "world");
Test t2 = new Test("world", "hello");
System.out.println(t1.equals(t2)); // false
System.out.println(t1.hashCode() == t2.hashCode()); // true
我们还在尊重hashCode
's contract吗?
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.
嗯,是的,因为它仅取决于 a
和 b
,并且我们正在使用他们的 hashCode
方法,我们可以假设该方法尊重合同。
If two objects are equal according to the
equals(Object)
method, then calling thehashCode
method on each of the two objects must produce the same integer result.
确实如此
Test t1 = new Test("hello", "world");
Test t2 = new Test("hello", "world");
System.out.println(t1.equals(t2)); // true
System.out.println(t1.hashCode() == t2.hashCode()); // true
It is not required that if two objects are unequal according to the
equals(java.lang.Object)
method, then calling thehashCode
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.
这就是我们最初试图展示的内容。这不是必需的。