[英]Query about internal implementation of HashMap
I am going through HashMap implementation and referring to this link: How does Java implement hash tables? 我正在研究HashMap的实现,并引用此链接: Java如何实现哈希表? I am finding that "A HashMap contains an array of buckets in order to contain its entries". 我发现“ HashMap包含一组存储桶以便包含其条目”。 So, I have few questions- 所以,我有几个问题-
3.In case of same hashcode for a key or collision it uses linked list.How it gets(search) the reference of the second, third node etc. 3,如果键或冲突的哈希码相同,则使用链表,它如何获取(搜索)第二,第三节点的引用等。
Thanks in adv. 感谢在广告中。
That depends on the map you make, if you make a HashMap<Integer, String>
then the buckets will be of those types, able to contain those types of objects 这取决于您创建的地图,如果您创建了HashMap<Integer, String>
则存储桶将属于这些类型,并且能够包含那些类型的对象
Because the drawbacks are worth it compared to the performance gain. 与性能提升相比,缺点是值得的。 Because arrays are a fixed size, a lot of checks can be skipped (ie does this index exist?). 由于数组是固定大小的,因此可以跳过很多检查(即,该索引是否存在?)。 You can read more about that here; 您可以在此处了解更多信息。 https://en.wikiversity.org/wiki/Java_Collections_Overview and Why not always use ArrayLists in Java, instead of plain ol' arrays? https://en.wikiversity.org/wiki/Java_Collections_Overview , 为什么不总是在Java中使用ArrayList而不是普通的ol'数组?
That is explained here better than I can; 这比我能解释的更好。 What happens when a duplicate key is put into a HashMap? 将重复的密钥放入HashMap后会发生什么?
If the hashmaps grows there is a very expensive rehash function, because the array has to grow to the next power of 2 aswell. 如果哈希图增加,那么将有一个非常昂贵的重新哈希函数,因为数组也必须增长到下一个2的幂。 In this case every bucket has to recalulate its index. 在这种情况下,每个存储桶都必须重新计算其索引。 In this case a new array is constructed. 在这种情况下,将构建一个新的数组。 This means there is no dynamic data structure needed. 这意味着不需要动态数据结构。
You can avoid rehashes if you create a new hashmap with a suitable capacity argument. 如果使用适当的容量参数创建新的哈希表,则可以避免重新哈希。
From the OpenJDK8 code source : 从OpenJDK8代码源 :
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.