[英]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.