I have been getting confused about some yourkit snapshots which seems to suggest that in a particular stack hashmap.put()
is proving to be costly.
Let's say the key for this map is a very complex object which has not overridden equals()
or hashCode()
Is it really possible for HashMap.hash()
or Object.hashCode()
to be costly in certain situations ?
It certainly is possible to create an object which is costly to put into a HashMap:
Obviously this is a contrived example but I've seen implementations that call methods on Hibernate backed, lazy loaded objects. Putting said object into a list resulted in numerous, quite expensive, database lookups.
Without overriding
hashCode()
the value is derived from the address of the object, which the JVM already has at the point the method is called, so it's (as good as) instant.