Search code examples
javadictionaryimmutabilitytreemapunmodifiable

Does an unmodifiable view of TreeMap retain the key ordering?


Suppose I have a TreeMap<Long,Long> map. I need to view an unmodifiable version from a getter so I return a Map<Long,Long> of return Collections.unmodifiableMap(map);.

However, I need it to be returned with the same ordering of keys as it was when it was a TreeMap<Long,Long>. Can I be guaranteed in all cases that the ordering of the keys will be the same in this unmodifiable Map when created from a TreeMap?


Solution

  • Yes

    It's a view. The underlying data structure is the same.