0

There are two hashmaps. Put and remove them for any number of times in the same order. Finally, foreach the hashmap. Are the output results of the two hashmaps consistent?

I think it should be consistent, but I can't find any data to clearly prove it

wcj
  • 1
  • 1
  • 3
    There's no guarantee about the iteration order of `HashMap` at all. That means you can't depend on two HashMaps that took the exact same operations on the same objects in the same JVM instance to produce the same iteration order. As a matter of implementation, I think that this *can* (and probably will) happen, but since it's not part of the spec that fact can easily change with the next JVM release. If you need *any guarantee* at all about traversal order then use something like `LinkedHashMap`. – Joachim Sauer Feb 02 '23 at 10:25
  • This is not answerable without a clear specification for "put and remove them for any number of times in the same order" – Stephen C Feb 02 '23 at 10:27
  • 1
    It might be interesting for you that a randomization did already exist in an earlier version. As [JDK-8006593](https://bugs.openjdk.org/browse/JDK-8006593) indicates, it has been removed due to performance considerations, not because it was contradicting the specification. So, randomization may come back at any time, if the implementors have a reason. – Holger Feb 02 '23 at 13:36
  • @Holger This is really a little relevant,but what the early randomization affected? – wcj Feb 02 '23 at 15:22
  • @Stephen C sorry for my weak English. For example, insert and delete the same key-value pairs from two maps in the same order, and finally traverse the two maps – wcj Feb 02 '23 at 15:27
  • What are the exact types of the keys? – Stephen C Feb 02 '23 at 15:31
  • @wjc - Look at the Java 7u source repo mirror on Github and track exactly what changes were made to the `HashMap` source code in 7u40. That is when the issue tracker says that the randomization was removed. – Stephen C Feb 02 '23 at 15:34
  • @StephenC Does the type of key affect this? Take String and Integer for example – wcj Feb 02 '23 at 15:36
  • 3
    Well yes ... but take an object which has an identity hashcode for a counter example. In that case the ordering will depend on the *actual* identity hashcodes ... and we need a clear understanding of what the OP *actually* means by "the same". But either way, the *precedent* of the randomization feature noted by Holger means that the answer to the OPs question is Java version specific ... and you can't rely on it not changing ... again. – Stephen C Feb 02 '23 at 22:58
  • 2
    There is no *guarantee* but results are often repeatable. They're repeatable often enough that people inadvertently depend on the ordering, as if it were guaranteed, until a change in ordering causes a bug. For example, HashMaps of different initial capacities might result in differing iteration orders even when the same mappings are added in the same order. – Stuart Marks Feb 03 '23 at 00:24
  • 2
    Randomization of iteration order is useful for preventing code from inadvertently depending on the iteration order. The maps produced by `Map.of` and `Map.copyOf` have randomized iteration order. We probably won't add randomization of HashMap itself because it would probably break too much code. – Stuart Marks Feb 03 '23 at 00:26

0 Answers0