简体   繁体   English

为什么 hashCode() 函数会产生错误

[英]Why does hashCode() function generate an error

import java.util.*;
public class Measurement {
int count;
    int accumulated;
    public Measurement() {}
    public void record(int v) {
        count++;
        accumulated += v;
    }
    public int average() {
        return accumulated/count;
    }
    public boolean equals(Object other) {
        if (this == other)
            return true;
        if (!(other instanceof Measurement))
            return false;
        Measurement o = (Measurement) other;
        if (count != 0 && o.count != 0)
            return average() == o.average();
        return count == o.count;
    }
    public int hashCode() {
        (1) INSERT CODE HERE 
    }
}

Which code, when inserted at (1), will provide a correct implementation of the hashCode() method in the following program?在 (1) 处插入哪些代码将提供以下程序中hashCode()方法的正确实现? Select the two correct answers.选择两个正确答案。

(a) return 31337;

(b) return accumulated / count;

(c) return (count << 16) ^ accumulated;

(d) return ~accumulated;

(e) return count == 0 ? 0 : average();

The correct answer is (a) and (e).正确答案是(a)和(e)。 (b) is incorrect because of count if it is 0, it will generate an arithmetic Exception, but I don't know the mean of (c) and (d). (b) 是不正确的,因为 count 如果它是 0,它会产生一个算术异常,但我不知道 (c) 和 (d) 的平均值。 Why they are false?为什么他们是假的?

Here is the general contract of Object.hashCode .这是Object.hashCode一般契约 The question is basically asking you which of those choices fulfils the general 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.每当在 Java 应用程序执行期间在同一对象上多次调用它时,hashCode 方法必须始终返回相同的整数,前提是对象上的 equals 比较中使用的信息没有被修改。 This integer need not remain consistent from one execution of an application to another execution of the same application.该整数不需要从应用程序的一次执行到同一应用程序的另一次执行保持一致。

  • 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.如果根据 equals(java.lang.Object) 方法两个对象不相等,则不需要对这两个对象中的每一个调用 hashCode 方法必须产生不同的整数结果。 However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.但是,程序员应该意识到为不相等的对象生成不同的整数结果可能会提高哈希表的性能。

Your understanding of why B is incorrect is correct.您对 B 为何不正确的理解是正确的。 C and D are incorrect because they do not fulfil the second point. C 和 D 不正确,因为它们不满足第二点。 That is, they do not work with equals .也就是说,它们不适用于equals

As you can see, the equals method compares the averages if the count s are both not 0, otherwise it compares the count s.如您所见,如果count s 都不为 0,则equals方法比较平均值,否则比较count s。

Option C is taking into account both the count and accumulated, so (accumulated: 6, count: 2) and (accumulated: 3, count: 1) would have unequal hash codes, but would be considered equal by equals .选项 C 同时考虑计数和累积,因此 (accumulated: 6, count: 2) 和 (accumulated: 3, count: 1) 将具有不相等的哈希码,但会被equals视为相等。

Option D is taking into account only the accumulated, so (accumulated: 6, count: 2) and (accumulated: 3, count: 1) would have unequal hash codes, but would be considered equal by equals .选项 D 仅考虑累计,因此 (accumulated: 6, count: 2) 和 (accumulated: 3, count: 1) 将具有不相等的哈希码,但会被equals视为相等。

Option A is a weird one.选项A很奇怪。 It returns a constant integer.它返回一个常量整数。 This means that all objects will have the same hash code.这意味着所有对象都将具有相同的哈希码。 Although it is not required by general contract to have a different hash code for different objects (see point 3), it is rarely useful to do this.虽然一般合同不要求为不同的对象使用不同的哈希码(见第 3 点),但这样做很少有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM