Does entrySet() in a LinkedHashMap also guarantee order?

前端 未结 3 897
Happy的楠姐
Happy的楠姐 2020-12-08 13:03

I am using a linkedHashMap to guarantee order when someone tries to access it. However, when it comes time to iterate over it, does using entrySet() to return key/value pair

相关标签:
3条回答
  • 2020-12-08 13:14

    If you're sure no changes will be made during the iteration, then proper ordering with entrySet() is guaranteed, as stated in the API.

    0 讨论(0)
  • 2020-12-08 13:34

    According to the Javadocs, yes.

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

    As for the edit, no, it should work just fine. But the entry set is somewhat faster since it avoids the overhead of looking up every key in the map during iteration.

    0 讨论(0)
  • 2020-12-08 13:39

    This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)

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