开发者

How to return an unmodifiable view of a Java Trove collection?

I'd like to put unmodifiable wrappers around some of the Trove collections: I've checked the Trove documentation and I cannot seem to find an easy way to do it (I may have overlooked something obvious).

So as of now every time I need such an unmodifiable wrapper I'm extending the Trove collection (for exam开发者_Python百科ple TIntLongHashMap) and delegating all the read-only calls to the Trove wrapped subject and throwing an UnsupportedOperationException in every method that tries to modify the collection.

Is there an easier way?

Note: this question is not about the default Java collections and, in this case, I'm not interested at all neither in the default Java collections nor in other Java collections: this question is specifically about Trove.


The accepted answer was correct at the time, but for anyone looking to do the same, Trove 3 now supports this via the TCollections class.

E.g.

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

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


There is no way to do this with the Trove API, only with the decorators.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜