本文介绍了HashMap如何在内部工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HashMap objHashMap = new HashMap();
objHashMap.put("key1", "Value1");
objHashMap.put("key1", "Value2");
System.out.println(objHashMap.get("key1"));
上面显示Value2的代码如何以及为什么
Above code displaying "Value2" how and why
推荐答案
因为哈希映射只为每个值存储唯一的键,这意味着你不能把两个相同名称的键,当你将覆盖值这个键,所以如果你想存储2个不同的值,你需要存储两个不同的键。
Because Hash maps store only unique keys for each value, this means that you can't put 2 keys with the same name in it, when you do you will overwrite the value for that key, so if you want to store 2 different values you need to store two different keys in it.
HashMap objHashMap = new HashMap();
objHashMap.put("key1", "Value1");
objHashMap.put("key2", "Value2"); //CHANGED THIS KEY to "key2"
System.out.println(objHashMap.get("key1"));
这篇关于HashMap如何在内部工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!