开发者

Collection.unmodifiableMap iteration order

Does Colelction.unmodifiableMap safeguard the iteration order?

I was trying newMap.put(key,Collections.ModifiableMap(oldMap)) Then when I do newMap.get(key) and iterate, iterate order seems to change.

How can we 开发者_StackOverflow社区protect the iterate order?


UnmodifiableMap simply delegates all methods, except writing ones. It's order is exactly that of the delegate.

If you need to have the same order as the first collection, use LinkedHashMap.


Check out Collections.unmodifiableSortedMap. That should provide you with a read-only view on your Map and maintain the sorted order of the keys.


If we look at the source for Collections.unmodifiableMap, we see that it just passes it to an UnmodifiableMap class, who simply wraps it. So it makes no changes to the underlying map's order.

And according to the documentation for unmodifiableMap it says it:

Returns an unmodifiable view of the specified map.

Since it says it returns a view, it is implying that we're not getting a different map, just a different way to access the old map.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜