Hacker News new | past | comments | ask | show | jobs | submit login

Can't use the object content since that can change.

It is not uncommon to use the state of an object to derive the hash code. It of course puts the burden to ensure that objects do not change in undesired ways while they are required to yield a stable hash code, for example while used as a key in a hash table, onto the developer. One common solution is to restrict the hash code calculation to immutable parts of the state, for example an ID.

The advantage of deriving the hash code from the state is that you can use the hash code to speed up object equality tests, i.e. if the hash codes of two objects do not match, they can not be equal.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: