Java 中 Map 的浅拷贝
据我了解,有几种方法(也许还有其他方法)可以在 Java 中创建 Map
的浅表副本:
Map<String, Object> data = new HashMap<String, Object>();
Map<String, Object> shallowCopy;
// first way
shallowCopy = new HashMap<String, Object>(data);
// second way
shallowCopy = (Map<String, Object>) ((HashMap<String, Object>) data).clone();
是否一种方法优于另一种方法,如果是,为什么?
值得一提的是,第二种方式给出了“Unchecked Cast”警告。所以你必须添加 @SuppressWarnings("unchecked") 来解决它,这有点令人恼火(见下文)。
@SuppressWarnings("unchecked")
public Map<String, Object> getDataAsMap() {
// return a shallow copy of the data map
return (Map<String, Object>) ((HashMap<String, Object>) data).clone();
}
As I understand it, there are a couple of ways (maybe others as well) to create a shallow copy of a Map
in Java:
Map<String, Object> data = new HashMap<String, Object>();
Map<String, Object> shallowCopy;
// first way
shallowCopy = new HashMap<String, Object>(data);
// second way
shallowCopy = (Map<String, Object>) ((HashMap<String, Object>) data).clone();
Is one way preferred over the other, and if so, why?
One thing worth mentioning is that the second way gives an "Unchecked Cast" warning. So you have to add @SuppressWarnings("unchecked")
to get around it, which is a little irritating (see below).
@SuppressWarnings("unchecked")
public Map<String, Object> getDataAsMap() {
// return a shallow copy of the data map
return (Map<String, Object>) ((HashMap<String, Object>) data).clone();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用复制构造函数进行复制总是更好。 Java 中的
clone()
已损坏(参见 SO:如何正确重写克隆方法?)。Josh Bloch 谈设计 - 复制构造函数与克隆
Bloch(顺便说一句,他设计并实现了 Collection 框架)甚至进一步表示,他只提供
clone()
方法只是“因为人们期望它”。他实际上根本不建议使用它。我认为更有趣的争论是复制构造函数是否比复制工厂更好,但这完全是一个不同的讨论。
It's always better to copy using a copy constructor.
clone()
in Java is broken (see SO: How to properly override clone method?).Josh Bloch on Design - Copy Constructor versus Cloning
Bloch (who by the way, designed and implemented the Collection framework) even went further in saying that he only provides the
clone()
method just "because people expect it". He does NOT actually recommend using it at all.I think the more interesting debate is whether a copy constructor is better than a copy factory, but that's a different discussion altogether.
两者都不是:构造函数<您所指的 /a> 是为 HashMap 实现定义的Map 的,(以及其他),但不适用于Map 接口本身(例如,考虑 Provider 的实现Map接口:你不会找到那个构造函数)。
另一方面,正如 Josh Bloch 所解释的,不建议使用
clone()
方法。关于 Map 接口(以及您的问题,您询问如何复制 Map,而不是 HashMap),您应该使用 Map#putAll():
例子:
Neither of the two: the constructor that you are referring to is defined for the HashMap implementation of a Map, (as well as for others) but not for the Map interface itself (for example, consider the Provider implementation of the Map interface: you will not find that constructor).
On the other hand it is not advisable to use the
clone()
method, as explained by Josh Bloch.In respect of the Map interface (and of your question, in which you ask how to copy a Map, not a HashMap), you should use Map#putAll():
Example:
在不知道其实现的情况下复制映射:
Copy a map without knowing its implementation: