我想将一个已经创建了一段时间的对象放入一个hashmap中,并且我希望它根据对象的当前状态来存储。
所以我想知道这个对象的哈希码是何时计算的:何时创建或放入映射中。而如果是第一种情况,如何存储当前状态。
非常感谢。
编辑:我实际上的意思是根据将其插入 map 时的状态来存储它。抱歉误导了。
最佳答案
每当任何代码调用对象的 hashCode()
方法(例如集合、容器或任何其他方法)时,都会检索对象的哈希码。
All the classes inherit a basic hash scheme from the fundamental base class
java.lang.Object
, but instead many override this to provide a hash function that better handles their specific data. Classes which provide their own implementation must override the object method public int hashCode().
引用号:java hashCode()
关于java - hashcode什么时候计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23318796/