How does Java HashMap store entries internally(Java HashMap 如何在内部存储条目)
问题描述
假设您有一个键类 (KeyClass),其中覆盖了 equals、hashCode 和 clone 方法.假设它有 2 个原始字段,一个 String(名称)和一个 int(id).
Say you have a key class (KeyClass) with overridden equals, hashCode and clone methods. Assume that it has 2 primitive fields, a String (name) and an int (id).
现在你定义
KeyClass keyOriginal, keyCopy, keyClone;
keyOriginal = new KeyClass("original", 1);
keyCopy = new KeyClass("original", 1);
keyClone = KeyClass.clone();
现在
keyOriginal.hashCode() == keyCopy.hashCode() == keyClone.hashCode()
keyOriginal.equals(keyCopy) == true
keyCopy.equals(keyClone) == true
所以就 HashMap 而言,keyOriginal、keyCopy 和 keyClone 是无法区分的.
So as far as a HashMap is concerned, keyOriginal, keyCopy and keyClone are indistinguishable.
现在,如果你使用 keyOriginal 将一个条目放入 HashMap,你可以使用 keyCopy 或 keyClone 将其取回,即
Now if you put an entry into the HashMap using keyOriginal, you can retrieve it back using keyCopy or keyClone, ie
map.put(keyOriginal, valueOriginal);
map.get(keyCopy) will return valueOriginal
map.get(keyClone) will return valueOriginal
此外,如果您在将键放入映射后对其进行变异,则无法检索原始值.所以对于例如
Additionally, if you mutate the key after you have put it into the map, you cannot retrieve the original value. So for eg
keyOriginal.name = "mutated";
keyOriginal.id = 1000;
Now map.get(keyOriginal) will return null
所以我的问题是
当你说 map.keySet() 时,它会返回地图中的所有键.HashMap 类如何知道 map 中存储的键、值和条目的完整列表是什么?
So my question is
when you say map.keySet(), it will return back all the keys in the map. How does the HashMap class know what are the complete list of keys, values and entries stored in the map?
编辑据我了解,我认为它可以通过将 Entry 键作为最终变量来工作.
EDIT So as I understand it, I think it works by making the Entry key as a final variable.
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
(docjar.com/html/api/java/util/HashMap.java.html).因此,即使我在将密钥放入地图后对其进行了变异,也会保留原始密钥.我的理解正确吗?但是即使保留了原始的键引用,仍然可以改变它的内容.那么如果内容发生了变异,而K,V仍然保存在原来的位置,那么检索是如何进行的呢?
(docjar.com/html/api/java/util/HashMap.java.html). So even if I mutate the key after putting it into the map, the original key is retained. Is my understanding correct? But even if the original key reference is retained, one can still mutate its contents. So if the contents are mutated, and the K,V is still stored in the original location, how does retrieval work?
编辑如果您在放入哈希映射后更改密钥,检索将失败.因此,不建议您使用可变的 hashmap 键.
EDIT retrieval will fail if you mutate the key after putting into the hashmap. Hence it is not recommended that you have mutable hashmap keys.
推荐答案
HashMap
维护一个条目表,其中包含对关联键和值的引用,根据它们的哈希码进行组织.如果你改变了一个键,那么哈希码会改变,但是HashMap
中的条目仍然按照原始的哈希码放在哈希表中.这就是 map.get(keyOriginal)
将返回 null 的原因.
HashMap
maintains a table of entries, with references to the associated keys and values, organized according to their hash code. If you mutate a key, then the hash code will change, but the entry in HashMap
is still placed in the hash table according to the original hash code. That's why map.get(keyOriginal)
will return null.
map.keySet()
只是遍历哈希表,返回它拥有的每个条目的键.
map.keySet()
just iterates over the hash table, returning the key of each entry it has.
这篇关于Java HashMap 如何在内部存储条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java HashMap 如何在内部存储条目
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01