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
For small hashCodes, the HashMap turns into an array (as this is what is used underneath) There is no requirement for it to do so but it happens to be the simplest implementation.
In short, if you add 0 to 10 to a HashSet or HashMap you will get them in order because the capacity is large enough to just layout those values in order.