HashMap
is not an ordered Collection
. (It is not even a Collection
) It is more like a dictionary.
If you want to perserve the order of the items you can either use a TreeMap
or a LinkedHashMap
.
The difference between these is that TreeMap
keeps them ordered by their natural sort order while LinkedHashMap
keeps the insertion order.
In most cases where you would like to have something like with using a List
you can use the LinkedHashMap
.
TreeMap
uses a red-black tree for its internal implementation while LinkedHashMap
uses doubly linked list.
My suggestion is that you should refer to the official documentation. There you will find thorough explanations.
Additionally the idiomatic way to iterate over Map
s is this:
for(Integer key : map.keySet()) {
String myString = map.get(key);
// ...
}