是否有一个通用的方法从集合/列表/集合...创建不可修改的列表/集合/地图? [英] Is there a generic way to create an un-modifiable List/Set/Map from Collection/List/Set... ?

查看:203
本文介绍了是否有一个通用的方法从集合/列表/集合...创建不可修改的列表/集合/地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java util Collections集合类提供在任何现有列表周围创建不可修改装饰器。但是,我们都知道(或在某一点学习困难的方式);它真的只是一个装饰器在列表周围,最初传递到该调用。装饰列表不能更改;

The java util Collections class offers to create an "unmodifiable" decorator around any existing list. But as we all know (or learn the hard way at some point); it is really just a decorator around the list that was originally passed into that call. The decorated list can't be changed; but it will change under the covers if the "original" list is modified.

现在假设我有一些类如

class Whatever {
   private final List<IDontCare> someElements;
   public Whatever(List<IDontCare> incomingElements) {
      someElements = unmodifiableCopyOf(incomingElements);

这个类简单想要使用一个真正不可修改的传入数据的副本。好主意 - 但似乎没有干净/通用的方法来实现该方法 unmodifiableCopyOf()

That class simple wants to use a really unmodifiable copy of the incoming data. Nice idea - but it seems that there is no clean/generic way to implement that method unmodifiableCopyOf().

可以想到:


  • 使用clone()创建副本...不幸的是,visibleclone只有具体的实现像ArrayList;但如果我只知道它是实现列表的东西...好吧;我无法克隆(很好地解释了此处

  • 只需创建一个中间容器;像新的ArrayList(incomingElements),并有装饰;但是如果incomingElements是一个链接列表呢?

  • 使用一些其他库,如Guava,提供高性能,不可变,随机访问List实现。但是,Guava在我的地方不是一个选择(我们非常局限于我们自己的库和一些Apache公共的东西)。

  • use "clone()" to create a copy ... unfortunately, a "visible" clone() exists only on concrete implementations like ArrayList; but if I only know "it is something implementing List" ... well; I can't clone (nicely explained here)
  • simply create an "intermediate container"; like new ArrayList(incomingElements) and have that "decorated"; but what if incomingElements was meant to be a linked list?
  • Use some other library, like Guava that provides "A high-performance, immutable, random-access List implementation". But well, Guava is not an option at my place (we are pretty much restricted to our own libraries and some Apache commons stuff).

Tl; dr:是真的没有通用解决方案(依赖于标准库)这个问题,给我一个真正不可修改的集合;基于某些其他集合?

Tl;dr: is there really no "generic" solution (relying on "standard libraries") to this problem what gives me a truly un-modifiable collection; based on some other collection?

推荐答案


是否有一种通用的方法来创建不可修改的列表/ Set / Map from Collection / List / Set ...?

Is there a generic way to create an un-modifiable List/Set/Map from Collection/List/Set… ?

是的!使用 Collections.unmodifiable ...

Set<String> stuff = Collections.<String>unmodifiableSet(oldSet);




装饰列表不能更改;但如果修改了原始列表,它将在封面下更改。

The decorated list can't be changed; but it will change under the covers if the "original" list is modified.

如果您不需要,在处理原始集合而不是新的集合的建设。您应该在构建时复制它。

If you don't want that then the fault is in the handling of the original collection rather than the construction of the new one. You should take a copy of it at construct time.

    Set<String> uset = Collections.<String>unmodifiableSet(new HashSet<>(oldSet));

这篇关于是否有一个通用的方法从集合/列表/集合...创建不可修改的列表/集合/地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆