You don't, basically. A HashMap
is fundamentally unordered. Any patterns you might see in the ordering should not be relied on.
There are sorted maps such as TreeMap
, but they traditionally sort by key rather than value. It's relatively unusual to sort by value - especially as multiple keys can have the same value.
Can you give more context for what you're trying to do? If you're really only storing numbers (as strings) for the keys, perhaps a SortedSet
such as TreeSet
would work for you?
Alternatively, you could store two separate collections encapsulated in a single class to update both at the same time?