Reputation: 6476
Let's say I have an object.
It contains width and height, x and y coordinates, and x and y representing velocity.
I have overridden equals method and I compare by comparing width, height, x and y and velocity.
What should I do with hash code?
The reason why I am confused is that it is a moving object and I am not sure what I should be using in order to calculate hash code, values are going to be constantly changing and the only thing that will remain static is size really.
Upvotes: 0
Views: 466
Reputation: 718926
To be honest the only reason why I wanted to implement hash code is because java rules state that if equals is overriden then hash code should be too. However none of the objects will be used in a map.
In that case, you could implement it as follows:
public int hashCode() {
throw new UnsupportedOperationException("hashCode is not implemented");
}
This has the advantage that if you accidentally use the object as a hash key you will get an immediate exception rather than unexpected behavior if the object is mutated at the wrong time.
Upvotes: 1
Reputation: 12305
Just make sure equals()
and hashCode()
are consistent.
That means, whatever fields are used to determine equality should be used to compute the hash code so that equal objects (with a certain state) will have the same hash code (for that same state).
But you may want to consider whether your equals()
implementation is correct in the first place, or whether you even need to override the default implementation.
Upvotes: 1
Reputation: 783
If you want your hash code code to compare equality then using the changing values will be just fine because the hash is calculated on the fly when you need it (or should be). So you have all these "moving" things and you want to know if they are equal (they have the same location or velocity or whatever) then it will be accurate.
If you want to use it in a hash table then don't override it and just use the default (The address in memory). Or if you want more control set a static counter and use that to create IDs for these objects.
Upvotes: 1
Reputation: 2854
According to Object.hashCode() there is a clause that can help with your decision:
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.
Since your equals()
compares width, height, x and y and velocity, your hashcode()
would not return the same hash whenever these values change.
Sample hashcode()
for you:
@Override
public int hashCode() {
int hash = 1;
hash = hash * 17 + width;
hash = hash * 31 + height;
hash = hash * 13 + x;
hash = hash * 13 + y;
hash = hash * 13 + velocity.hashcode();
return hash;
}
You can go further by storing the hashcode in a private variable, and use a dirty
flag to know to recalculate the hashcode
of your object if any parameters change. Since you are not doing anything expensive within the hashcode()
method, I don't think you would need to do that.
Upvotes: 2
Reputation: 3806
Typically, I would assign an ID to the object as a member variable and return the ID as the hashcode value.
Upvotes: 0