In Java, the keys in a hashing data structure can be mutable, but it is generally not recommended to use mutable objects as keys.
The reason is that the hash code of an object is usually calculated based on its state at the time when the hash code is generated. If the object’s state changes while it is being used as a key in a hash table, then the hash code of the key may no longer match its original hash code, which can cause problems in the hash table’s internal data structure and result in incorrect behavior.
To avoid these issues, it is recommended to use immutable objects as keys in hashing data structures. If you do need to use a mutable object as a key, it is important to ensure that the object’s hash code and equals method are implemented correctly and take into account all of the object’s mutable state. Additionally, you should avoid modifying the object while it is being used as a key in the hash table to prevent any issues with the hash table’s internal data structure
You may also like:
What are common multi-threading issues faced by Java Developers?
What are different states of a Thread? What does those states tell us?
What happens when wait() & notify() methods are called?
What is difference between sleep(), yield() and wait() method?
What is difference between Callable and Runnable Interface?