Will hashmap.keyset() return keys in the order they were added to the hashmap?

前端 未结 1 2027
渐次进展
渐次进展 2021-02-09 16:10

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 o

1条回答
  •  被撕碎了的回忆
    2021-02-09 16:56

    [...] 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). [...]

    0 讨论(0)
提交回复
热议问题