Alex Rivera | Logout

Does entrySet() in a LinkedHashMap also guarantee order?

Asked 2009-07-27T19:21:25.353
91

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 pairs guarantee order as well? No changes will be made while iterating.

EDIT: Also, are there any adverse effects from iterating through the map by iterating through its keys and calling get?

Edit
Report

2 Answers

2

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

answered 2009-07-27T19:27:52.440
1

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

answered 2009-07-27T19:24:33.537

Your Answer