Reputation: 23901
I know that the .keySet()
returns a set, which is un-ordered.
As far as I can tell, that means I need to keep an array list of keys in order to track the order in which the keys were added to the hashmap, correct?
Upvotes: 6
Views: 8099
Reputation: 421020
[...] that means I need to keep an array list of keys in order to track the order in which the keys were added to the hashmap, correct?
Yes, that's correct. Or, you could use a LinkedHashMap
which does this for you.
From the documentation:
[...] 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). [...]
Upvotes: 10