6

Some test cases are failing in my application which depends upon the element insertion order . It used to work fine in Java 7 but this issue started after upgrading to Java 8. While searching internet I found this in an article:

Java 8 includes some possible changes to HashSet/Map iteration order:

Can some please suggest me - how can I iterate the objects in a Map in the same order as the insertion order into the Map, considering I would still be using Java 1.8 in my dev environment?

Yes of course it was never assured by HashMap that the objects can be retrieved in the same order , but yes it used to work in java 7.

Does LinkedHashMap work to implement this ?

Mifeet
  • 12,949
  • 5
  • 60
  • 108
Siddhartha
  • 492
  • 1
  • 5
  • 15
  • 1
    What does the `LinkedHashMap` documentation tell you? – RealSkeptic May 14 '16 at 13:54
  • 6
    Have you checked what Java 7 `HashMap` documentation says ?: https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html : **This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.** You must use other collection that guarantes the order - LinkedHashMap – krokodilko May 14 '16 at 13:56
  • 1
    Use a `TreeMap` instead from the javaDocs: A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. – Jorge Campos May 14 '16 at 13:58
  • @RealSkeptic - it tells that , it will iterate in the order in which the entries were put into the map . I think `LinkedHahMap` will solve this problem - but still wanted to confirm it if anyone has already faced this issue and fixed it. – Siddhartha May 14 '16 at 13:58
  • 4
    The proper way to confirm it is to *try* it. It's a lot less efficient to ask humans if this will work than to simply try it in your exact situation on the machine. – RealSkeptic May 14 '16 at 14:01

1 Answers1

9

Yes, you must use LinkedHashMap which has a stable iteration order even across Java versions, as enforced by its contract:

This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map.

On several occasions, we also needed repeatable iteration order across different Java versions and LinkedHashMap worked just fine.


TreeMap would also be a solution for stable iteration order. Of course, it has a logarithmic operation time (as opposed to constant in LinkedHashMap) and the iteration order is not insertion order but key order:

The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time.

Mifeet
  • 12,949
  • 5
  • 60
  • 108