What causes the slightly unpredictable ordering of the iterator() for the java.util.HashSet and HashMap.keySet() classes?

前端 未结 4 1786
情歌与酒
情歌与酒 2020-12-15 10:45

Six years ago, I burned several days trying to hunt down where my perfectly deterministic framework was responding randomly. After meticulously chasing the entire framework

4条回答
  •  自闭症患者
    2020-12-15 11:21

    http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#hashCode() says:

    As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

    So maybe the internal address changes?

    This also means that you could propably fix it without giving up speed by writing your own hashCode() method for everything that should act as a key.

提交回复
热议问题