简体   繁体   中英

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? 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). (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). Why they are false?

Here is the general contract of 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. 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.

  • 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.

Your understanding of why B is incorrect is correct. C and D are incorrect because they do not fulfil the second point. That is, they do not work with 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.

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 .

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 .

Option A is a weird one. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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