Google News
logo
Java Collections - Interview Questions
What is the quality of a good key for HashMap?
Understanding how HashMap works, it is easy to know that they depend mainly on equals and hashCode methods of key objects. So, a good key must provide the same hashCode over and over again irrespective of the times it is fetched.
 
In the same way, when compared with the equals method, the same keys must return true and different keys must return false. That’s why the best candidate for HashMap keys is said to be immutable classes.
Advertisement