如何返回 Java Trove 集合的不可修改视图?
我想在一些 Trove 集合周围放置不可修改的包装器:我已经检查了 Trove 文档,但似乎找不到一种简单的方法来做到这一点(我可能忽略了一些明显的事情)。
因此,到目前为止,每次我需要这样一个不可修改的包装器时,我都会扩展 Trove 集合(例如 TIntLongHashMap),并将所有只读调用委托给 Trove 包装的主题并抛出 尝试修改集合的每个方法中都会出现 UnsupportedOperationException。
有更简单的方法吗?
注意:这个问题与默认的 Java 集合无关,在这种情况下,我对默认的 Java 集合和其他 Java 集合都不感兴趣:这个问题专门与 Trove 有关。
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 example 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当时接受的答案是正确的,但对于任何想要做同样事情的人来说,Trove 3 现在通过
TCollections
类支持这一点。例如
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.
使用 Trove API 无法做到这一点,只能使用装饰器。
There is no way to do this with the Trove API, only with the decorators.