HashMap does not guarantee preservation of the insertion order.
Use LinkedHashMapif you need such a guarantee.
This implementation differs from HashMap in that it maintains a double-linked list that goes through all its entries. This linked list defines the iteration order, which is usually the order in which the keys were inserted into the map (insertion order).
source
share