I thought that HashMap is unordered, and when iterating over the keys, you can\'t know what will be the order? In this example, it looks like the map is already sorted by the ke
True but there is no guarantee of maintaining that order.
From Hashmap docs
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.
Your bench mark is not enough to decide over it.
Look at TreeMap If you need the sorting order
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