java - 如何将内容从以前的 hashmap 传输到新的 hashmap?

标签 java hashmap

  HashMap<String,Integer> map= new HashMap<String,Integer>();
  map.put("first",1);
  map.put("second",2);
  map.put("third",3);

  HashMap<String,Integer> map2= new HashMap<String,Integer>();
  map2= map.clone();

我的问题是如何将项目从 map 转移到 map2?我的代码正确吗?

最佳答案

很简单,使用参数化构造函数

  HashMap<String,Integer> map2= new HashMap<String,Integer>(map);

关于java - 如何将内容从以前的 hashmap 传输到新的 hashmap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18328176/

相关文章:

java - java中Hashmap和ArrayList添加值的方式不同

java - Java中: List, ArrayList和Map、HashMap

java - twitter4j 未找到身份验证挑战

java - 如何直接在 android 中从 HashMap 设置对象字段

java - Java Map<String, Object> 的 C++ 等价物

java - 为什么 HashMap 会重新哈希键对象提供的哈希码?

java - Spring 启动 : redirect from HTTP to HTTPS results in 405 error for PUT method

java - 使用选择排序查找数组的中位数

java - Android Studio Java 中的两个变量求和

java - BeanUtils 中转换器的注册是线程本地的吗?