Does Java's LinkedHashMap maintain the order o

2019-01-17 19:28发布

When LinkedHashMap.keySet() is called, will the order of the Set returned be the same as the order the keys were added in?

2条回答
别忘想泡老子
2楼-- · 2019-01-17 19:58

Yes. The exception is that when a key is reinserted, it appears in the order in which it was first inserted to the list.

查看更多
【Aperson】
3楼-- · 2019-01-17 20:10

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.

查看更多
登录 后发表回答