HashMap中数据的存储模式
一般数据结构均由数组和链表组成,HashMap的存储模式也不列外:数组+链表 在JDK1.8中不同于之前的版本,其数据存储在内置类Node或TreeNode中。 实际上通过源码可以发现,TreeNode是Node的子类。也就是说,数据实际上还是存储在Node中。 Node的定义:
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
......
}
TreeNode的定义:
static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
TreeNode<K,V>
parent;
TreeNode<K,V> left;
TreeNode<K,V> right;
TreeNode<K,V> prev;
boolean red;
.......
}
在源代码中我们能看到如下的定义:
transient Node<K,V>[]
table;
当我们往HashMap放入数据时,他会根据KEY的hash值与数组容量进行计算(hash
转载请注明原文地址: https://ju.6miu.com/read-14711.html