简体   繁体   English

从Java Map添加*副本*条目 <String, Object> 属性映射

[英]Adding *copies* of entries from Java Map<String, Object> propertyMap

I would like to add copies of a propertyMap to my propertyMap: 我想将propertyMap的副本添加到我的propertyMap:

  public void addProperties(Map<String, Object> propertyMap) {
    for (Map.Entry<String, Object> propertyEntry : propertyMap.entrySet()) {
      this.propertyMap.put(propertyEntry.getKey(), propertyEntry.getValue());
    }
  }

The code above does not do that but hopefully conveys the intent? 上面的代码没有这样做,但希望传达意图?

What's the best way to do this? 最好的方法是什么? I have done some reading on "cloning", "defensive copying", "immutable objects", Collections.unmodifiable... and the like but I am more confused than before. 我已经完成了一些关于“克隆”,“防御性复制”,“不可变对象”,Collections.unmodifiable ......之类的阅读,但我比之前更加困惑。

All I need, in typical SO style, is a better way to write what I mean in the code snippet, please. 我所需要的只是典型的SO风格,是一种更好的方式来表达我在代码片段中的意思。

It looks like you can just use putAll : 看起来你可以使用putAll

public void addProperties(Map<String, Object> propertyMap) {
    this.propertyMap.putAll(propertyMap);
}

This is called "defensive copying". 这被称为“防御性复制”。 What happens here is the values in the local propertyMap are copied into the instance's propertyMap . 这里发生的是本地propertyMap中的值被复制到实例的propertyMap A weakness here is that changes the given propertyMap aren't going to be reflected in the instance's propertyMap . 这里的一个弱点是给定的propertyMap更改不会反映在实例的propertyMap This is essentially creating a snapshot of the given map and copying that snapshot to the instance field map. 这实质上是创建给定地图的快照并将该快照复制到实例字段映射。

There are other ways of creating defensive copies as well, including clone() and the HashMap(Map) constructor. 还有其他创建防御性副本的方法,包括clone()HashMap(Map)构造函数。

For immutable collections, the unmodifiable methods in Collections will return collections that throw exceptions when you try to add to them. 对于不可改变的集合,在不可修改的方法Collections将返回当您尝试添加到他们抛出异常的集合。 For example, 例如,

Set<String> strs = Collections.unmodifiableSet(new HashSet<String>());
strs.add("Error"); // This line throws an exception

Immutable collections protect their values by disallowing modification (removing and adding) while defensive copies protect their values by not referencing the copied collection (in other words, changes in the original collection aren't shown in the copy). 不可变集合通过禁止修改(删除和添加)来保护其值,而防御副本通过不引用复制的集合来保护其值(换句话说,原始集合中的更改未显示在副本中)。

I think for each key you don't have to worry about making copies because they are immutable. 我认为对于每个键,您不必担心复制,因为它们是不可变的。 But for the values it depends on what type objects they are. 但对于值,它取决于它们是什么类型的对象。 If they are mutable objects then you have to make copies of all of them. 如果它们是可变对象,那么你必须复制所有这些对象。

public void addProperties(Map<String, Object> propertyMap) { 
    Cloner cloner = new Cloner();
    for (Map.Entry<String, Object> propertyEntry : propertyMap.entrySet()) { 
        this.propertyMap.put(propertyEntry.getKey(), cloner.deepClone(propertyEntry.getValue())); 
    } 
} 

You can check this for deep clonning Deep clone utility recomendation . 您可以检查此深层克隆深度克隆实用程序推荐

From the homepage http://code.google.com/p/cloning/ 从主页http://code.google.com/p/cloning/

IMPORTANT : deep cloning of Java classes might mean thousands of objects are cloned! 重要信息:深入克隆Java类可能意味着克隆了数千个对象! Also cloning of files and streams might make the JVM crash. 克隆文件和流可能会导致JVM崩溃。 Enable dumping of cloned classes to stdout during development is highly recommended in order to view what is cloned. 为了查看克隆的内容,强烈建议在开发期间将克隆类转储到stdout。

So, it's good to know what you are trying to clone. 所以,知道你要克隆什么是很好的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 初始化地图 <String, Object> 来自地图条目的实例 - Initialize Map<String, Object> instance from Map entries Java:在两个列表之间获取不同的条目Map <String, Object> - Java: Get different entries between two lists Map<String, Object> Java Stream 比较和过滤来自 Map 的条目<string, map<string, list<string> &gt;&gt;</string,> - Java Stream compare and filter entries from Map<String, Map<String, List<String>>> 将对象添加到多个java集合中:这是否会生成多个副本? - Adding an object to multiple java collections: Does this make multiple copies? 在Java 8中从列表创建字符串和对象的映射 - Creating a map of String and Object from a List in Java 8 Java - Map <string, list<object> &gt; 遍历列表并跳过前 N 个条目</string,> - Java - Map<String, List<Object>> iterate through list and skip first N entries 使用Java 8进行过滤:地图 <String, Set<Object> &gt;从地图 <String, Set<Object> &gt;基于对象的属性 - Filtering with Java 8: Map<String, Set<Object>> from Map<String, Set<Object>> based on an attribute of Object 在 ConcurrentMap 中填充条目<string, completablefuture<object> &gt; 在 java</string,> - Populating entries in ConcurrentMap<String, CompletableFuture<Object>> in java Java - 未选中从Object转换为Map.Entry <String,String> - Java - Unchecked cast from Object to Map.Entry<String,String> 解析Java地图 <string, string> 从XML到C#Object - Parse Java Map<string, string> from XML into C# Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM