问题
I have a question, if we need to pass custom object as a key, we just need to override equals and hashcode methods or even the class should be immutable?
because string is immutable so we prefer it as a key in hashmap, so with that logic i have raised this question ?
回答1:
It depends on your hashing function.
All data that is being hashed should be immutable. Otherwise you will lose access to your value once you alter one of those fields, until you pass another object which hashes the same way. The odds of finding your value again using any other key object is quite low.
来源:https://stackoverflow.com/questions/41299460/should-hashmap-key-be-immutable