How to return the immutable look of the Java Trove collection?

I would like to put non-modifiable wrappers around some Trove collections: I checked the Trove documentation and I cannot find an easy way to do this (maybe I missed something obvious).

So, every time I need such an unmodifiable shell, I expand the Trove collection (for example, TIntLongHashMap) and delegate all read-only calls to the object wrapped in Trove, and throw an UnsupportedOperation exception in every method that tries to change the collection.

Is there an easier way?

Note. . This question does not apply to standard Java collections, and in this case, I am not interested in either the default Java builds or other Java collections: this question is specifically about Trove.

+3
source share
2 answers

It is not possible to do this using the Trove API, only with decorators.

+1
source

At that time, the accepted answer was correct, but for those who want to do the same, Trove 3 now supports this through the class TCollections.

eg.

TIntLongMap myMap = new TIntLongHashMap();
TIntLongMap myUnmodifiableMap = TCollections.unmodifiableMap(myMap);

myUnmodifiableMap.put(1, 2L); // throws UnsupportedOperationException
+3
source

Source: https://habr.com/ru/post/1730220/


All Articles