So I read about HashMap. At one point it was noted:
“Immutability also allows caching the hashcode of different keys which makes the overall retrieval process very fast and suggest that String and various wrapper classes (e.g.,
Integer) provided by Java Collection API are very goodHashMapkeys.”
I don’t quite understand… why?
String#hashCode:Since the contents of a
Stringnever change, the makers of the class chose to cache the hash after it had been calculated once. This way, time is not wasted recalculating the same value.