Is the order guaranteed for the return of keys and

2019-01-03 08:30发布

I know LinkedHashMap has a predictable iteration order (insertion order). Does the Set returned by LinkedHashMap.keySet() and the Collection returned by LinkedHashMap.values() also maintain this order?

7条回答
仙女界的扛把子
2楼-- · 2019-01-03 09:22

AFAIK it is not documented so you cannot "formally" assume so. It is unlikely, however, that the current implementation would change.

If you want to ensure order, you may want to iterate over the map entires and insert them into a sorted set with an order function of your choice, though you will be paying a performance cost, naturally.

查看更多
登录 后发表回答