Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmodifiable NavigableSet/NavigableMap in Java?

java.util.Collections has several unmodifiable methods that provide unmodifiable collection views by wrapping collections in decorators that prohibit mutation operations.

Java 6 added support for java.util.NavigableSet and java.util.NavigableMap.

I'd like to be able to have unmodifiable NavigableSets and NavigableMaps, but java.util.Collections#unmodifiableSortedSet(SortedSet) and java.util.Collections#unmodifiableSortedMap(SortedMap) are not sufficient because they do not support the operations that are particular to NavigableSet and NavigableMap.

Are there de-facto implementations for unmodifiableNavigableSet and unmodifiableNavigableMap?

like image 908
Greg Mattes Avatar asked Apr 05 '10 09:04

Greg Mattes


2 Answers

Guava now provides (since version 12.0, released April 30, 2012) Maps.unmodifiableNavigableMap and Sets.unmodifiableNavigableSet.

like image 65
Olivier Cailloux Avatar answered Oct 30 '22 08:10

Olivier Cailloux


Java SE 8 included these two methods.

See the Javadoc.

like image 10
JodaStephen Avatar answered Oct 30 '22 09:10

JodaStephen