Java HashMap 如何在内部存储条目

How does Java HashMap store entries internally(Java HashMap 如何在内部存储条目)

本文介绍了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 如何在内部存储条目

基础教程推荐