How does the JVM ensure that System.identityHashCode() will never change?

前端 未结 5 930
终归单人心
终归单人心 2020-11-29 00:33

Typically the default implementation of Object.hashCode() is some function of the allocated address of the object in memory (though this is not

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 00:39

    The general guideline for implementing a hashing function is :

    • the same object should return a consistent hashCode, it should not change with time or depend on any variable information (e.g. an algorithm seeded by a random number or values of mutable member fields
    • the hash function should have a good random distribution, and by that I mean if you consider the hashcode as buckets, 2 objects should map to different buckets (hashcodes) as far as possible. The possibility that 2 objects would have the same hashcode should be rare - although it can happen.

提交回复
热议问题