Use LinkedHashMap
instead of HashMap
. The former preserves the order of the insertion of the elements.
From its JavaDoc (emphasis mine):
Hash table and linked list implementation of the Map interface, with predictable iteration order. 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).
This will make your Map
and your List
(backed by an ArrayList
) have the elements in the same order.
Not sure how have you designed your app, but I recommend you to program oriented to interfaces (Map
, List
, etc...) instead of classes (HashMap
, ArrayList
, etc...). More info: What does it mean to "program to an interface"?