问题
When LinkedHashMap.keySet() is called, will the order of the Set returned be the same as the order the keys were added in?
回答1:
Yes.
See: LinkedHashMap:
This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
and from the HashMap#keySet documentation:
The set [returned] is backed by the map, so changes to the map are reflected in the set, and vice-versa.
回答2:
Yes. The exception is that when a key is reinserted, it appears in the order in which it was first inserted to the list.
来源:https://stackoverflow.com/questions/3478061/does-javas-linkedhashmap-maintain-the-order-of-keys