java - .putAll() 会影响复制的变量吗?
问题描述
我完全不知所措。这是我的代码
HashMap>> themap = new HashMap>>(); HashMap>outerMap = new HashMap>();
...
//themap is populated through OnCreate(), and works fine.
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
String currentfragment = "my_string"
...
HashMap<String, HashMap<String, HashMap<String, Integer>>> copymap = copy(themap);
System.out.println("1" + themap);
outerMap.putAll(copymap.get(currentfragment));
System.out.println("2" + themap);
...
}
在另一种方法中,改变一些值themap
。问题是当 onNavigationItemSelected 被调用时,第一个.println()
打印正确的地图,改变了值,第二个.println()
打印themap
了它的起始值,在它们被改变之前。我做错了什么?
编辑:我已经按照How to copy HashMap (not shallow copy) in Java的说明进行操作,但它仍然不起作用。我已经更新了我以前的代码,这是我的copy
方法:
public static HashMap<String, HashMap<String, HashMap<String, Integer>>> copy(HashMap<String, HashMap<String, HashMap<String, Integer>>> original) {
HashMap<String, HashMap<String, HashMap<String, Integer>>> copy = new HashMap<>();
for (Map.Entry<String, HashMap<String, HashMap<String, Integer>>> entry : original.entrySet()) {
HashMap<String, HashMap<String, Integer>> copyInnerMap = new HashMap<>();
for (Map.Entry<String, HashMap<String, Integer>> innerEntry : entry.getValue().entrySet()) {
HashMap<String, Integer> innerInnerMap = innerEntry.getValue();
HashMap<String, Integer> copiedInnerInnerMap = new HashMap<>();
for (Map.Entry<String, Integer> innerInnerMapEntry : innerInnerMap.entrySet()) {
copiedInnerInnerMap.put(innerInnerMapEntry.getKey(), innerInnerMapEntry.getValue());
}
copyInnerMap.put(innerEntry.getKey(), copiedInnerInnerMap);
}
copy.put(entry.getKey(), copyInnerMap);
}
return copy;
}
EDIT2:我已经更新了我的复制方法,使其足够深。
这是第一个输出:
1{compu={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheetsfilled=0, Fun=0}, Day2={Work hours=0, Study hours =0, 日成绩=-1, 填表=0, 乐趣=0}, 第 3 天={工作时间=0, 学习时间=0, 日成绩=-1, 填表=0, 乐趣=0}}, 化学={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheetsfilled=0, Fun=0}, Day2={Work hours=0, Study hours=0, Day grades=-1,填满的表格=0,乐趣=0},第 3 天={工作时间=0,学习时间=0,日成绩=-1,填满的表格=0,有趣=0}},数学={第 1 天={工作时间=0 , 学习时间=0, 一天成绩=-1, 填满表格=0, 乐趣=0},第二天={工作时间=0, 学习时间=0, 一天成绩=-1, 填满表格=0, 有趣=2}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheetsfilled=0, Fun=0}}, physic={Day1={Work hours=0, Study hours=0, Day grades= -1, 填满表格=0, 乐趣=0}, Day2={工作时间=0, 学习时间=0, 日成绩=-1, 填满表格=0, Fun=0}, Day3={工作时间=0,学习时间=0,一天成绩=-1,填写的表格=0,乐趣=0}},无={Day1={工作时间=0,学习时间=0,一天成绩=-1,填写的表格=0,有趣=0},第 2 天={工作时间=0,学习时间=0,日成绩=-1,填满的表格=0,乐趣=2},第 3 天={工作时间=0,学习时间=0,日成绩=- 1,床单填充=0,乐趣=0}}}
第二个:
2{compu={Day1={Work hours=0, Study hours=0, Day grades=-1, Sheetsfilled=0, Fun=0}, Day2={Work hours=0, Study hours=0 , 日成绩=-1, 填满的表格=0, 乐趣=0}, 第 3 天={工作时间=0, 学习时间=0, 日成绩=-1, 填满的表格=0, 有趣=0}}, 化学={第 1 天={工作时间=0,学习时间=0,日成绩=-1,填满表格=0,乐趣=0},第 2 天={工作时间=0,学习时间=0,日成绩=-1,填满表格=0, 乐趣=0}, Day3={工作时间=0, 学习时间=0, 日成绩=-1, 填满的表格=0, 乐趣=0}}, 数学={Day1={工作时间=0, 学习小时=0,一天成绩=-1,填写的表格=0,乐趣=0},第二天={工作时间=0,学习时间=0,一天成绩=-1,填写的表格=0,乐趣=0}, Day3={Work hours=0, Study hours=0, Day grades=-1, Sheetsfilled=0, Fun=0}}, physic={Day1={Work hours=0, Study hours=0, Day grades= -1, 填满表格=0, 乐趣=0}, Day2={工作时间=0, 学习时间=0, 日成绩=-1, 填满表格=0, Fun=0}, Day3={工作时间=0,学习时间=0,一天成绩=-1,填写的表格=0,乐趣=0}},无={Day1={工作时间=0,学习时间=0,一天成绩=-1,填写的表格=0,有趣=0},第 2 天={工作时间=0,学习时间=0,日成绩=-1,填满表格=0,乐趣=0},第 3 天={工作时间=0,学习时间=0,日成绩=- 1,床单填充=0,乐趣=0}}}
解决方案
由于您有 2 级哈希图,因此您复制它们的深度不够。尝试用outerMap.putAll(copymap.get(currentfragment));
以下代码 替换您的
HashMap<String, HashMap<String, Integer>> sample = themap.get(currentfragment);
for (String s : sample.keySet()) {
outerMap.put(s, new HashMap<>(sample.get(s)));
}
推荐阅读
- sql - 获取特定条件下的 Pivot 值
- sql - 如何在pl sql中编写一个每30个字符换行的函数
- css - 如何设置边距值的scss动态类名
- jquery - Typescript 在添加 jquery 时失败,无法解析命名空间 Sizzle
- javascript - 只防止文本区域中的空格
- reactjs - 为什么我需要测试 Redux 实现,而我只能使用测试库测试 DOM?
- r - 将姓名与电子邮件匹配(如果准确)
- c# - AutoMapper 不更新 Collection 项目
- c++ - C++:进程以退出代码 139 完成(被信号 11:SIGSEGV 中断)
- ansible - 没有用户名和密码的ansible telnet