0
点赞
收藏
分享

微信扫一扫

HashMap(jdk1

GG_lyf 2022-08-13 阅读 54

final int hash;

final K key;

V value;

Node<K,V> next;

Node(int hash, K key, V value, Node<K,V> next) {

this.hash = hash;

this.key = key;

this.value = value;

this.next = next;

}

public final K getKey() { return key; }

public final V getValue() { return value; }

public final String toString() { return key + "=" + value; }

public final int hashCode() {

return Objects.hashCode(key) ^ Objects.hashCode(value);

}

public final V setValue(V newValue) {

V oldValue = value;

value = newValue;

return oldValue;

}

public final boolean equals(Object o) {

if (o == this)

return true;

if (o instanceof Map.Entry) {

Map.Entry<?,?> e = (Map.Entry<?,?>)o;

if (Objects.equals(key, e.getKey()) &&

Objects.equals(value, e.getValue()))

return true;

}

return false;

}

}

红黑树(不详细介绍了,我自己也没有弄懂呢):

static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {

TreeNode<K,V> parent; // red-black tree links

TreeNode<K,V> left;

TreeNode<K,V> right;

TreeNode<K,V> prev; // needed to unlink next upon deletion

boolean red;

TreeNode(int hash, K key, V val, Node<K,V> next) {

super(hash, key, val, next);

}

}

介绍几个常量:

//默认数组容量大小: 16

static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

//数组最大容量大小:2的30次方

static final int MAXIMUM_CAPACITY = 1 << 30;

//默认的加载因子

static final float DEFAULT_LOAD_FACTOR = 0.75f;

//使用树而不是链表的计数阈值,将元素添加到至少这么多的节点的链表中时,链表将装换为树

static final int TREEIFY_THRESHOLD = 8;

//在哈希表扩容时,如果发现链表长度小于 6,则会由树重新退化为链表。

static final int UNTREEIFY_THRESHOLD = 6;

//可以进行树化的最小数组容量

static final int MIN_TREEIFY_CAPACITY = 64;

//存储键值对元素的数组,分配后,长度始终是2的幂(哈希桶数组)

transient Node<K,V>[] table;

//此映射中包含的键-值映射数,即当前数组中的元素数量

transient int size;

//主要用于记录HashMap内部结构发生变化的次数。

transient int modCount;

//哈希表所能容纳的最大键值对个数,下次扩容的临界值,size>=threshold 数组就会扩容

int threshold;

//负载因子

final float loadFactor;

其中threshold = capacity * loadFactory

构造方法:

HashMap有四个构造方法:

public HashMap(int initialCapacity, float loadFactor) {

if (initialCapacity < 0)

throw new IllegalArgumentException("Illegal initial capacity: " +

initialCapacity);

if (initialCapacity > MAXIMUM_CAPACITY)

initialCapacity = MAXIMUM_CAPACITY;

if (loadFactor <= 0 || Float.isNaN(loadFactor))

throw new IllegalArgumentException("Illegal load factor: " +

loadFactor);

this.loadFactor = loadFactor;

this.threshold = tableSizeFor(initialCapacity);

}

public HashMap(int initialCapacity) {

this(initialCapacity, DEFAULT_LOAD_FACTOR);

}

public HashMap() {

this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted

}

public HashMap(Map<? extends K, ? extends V> m) {

this.loadFactor = DEFAULT_LOAD_FACTOR;

putMapEntries(m, false);

}

initialCapacity?初始容量

官方要求我们要输入一个2的N次幂的值,比如说2、4、8、16等等这些,但是我们忽然一个不小心,输入了一个20怎么办?没关系,虚拟机会根据你输入的值,找一个离20最近的2的N次幂的值,比如说16离他最近,就取16为初始容量。

loadFactory负载因子:

默认是0.75.? 负载因子表示一个散列表的空间的使用程度,有这样的一个公式:

initailCapacity*loadFactor = HashMap?的容量。

所以负载因子越大则散列表的装填程度越高,也就是能容纳更多元素,元素多了,链表大了,所以此时索引效率就会降低。反之,负载因子越小则链表中的数据量就越稀疏,此时会对空间造成浪费,但此时索引效率高。

确定哈希桶数组索引位置

jdk1.8版本中将计算数组位置的过程融入到了put()过程中。

static final int hash(Object key) {

int h;

//h = key.hashCode()为第一步取hashCode值

//h ^ (h >>> 16)为第二步 高位参与运算

return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);

}

//确定hash值之后的操作就是确定在哈希桶中的位置了

p = tab[i = (n - 1) & hash]

下面举个例子:?

HashMap(jdk1

应为数组的长度都是2的幂数,减一后都是1,因此hash算法最终得到的index结果,完全取决于key的HashCode值得最后几位。

put插入过程

具体的流程如下图:

HashMap(jdk1

public V put(K key, V value) {

return putVal(hash(key), key, value, false, true);

}

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,

boolean evict) {

Node<K,V>[] tab; Node<K,V> p; int n, i;

if ((tab = table) == null || (n = tab.length) == 0)

n = (tab = resize()).length;

if ((p = tab[i = (n - 1) & hash]) == null)

tab[i] = newNode(hash, key, value, null);

else {

Node<K,V> e; K k;

if (p.hash == hash &&

((k = p.key) == key || (key != nu

HashMap(jdk1

ll && key.equals(k))))

e = p;

else if (p instanceof TreeNode)

e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);

else {

for (int binCount = 0; ; ++binCount) {

if ((e = p.next) == null) {

p.next = newNode(hash, key, value, null);

if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st

treeifyBin(tab, hash);

break;

}

if (e.hash == hash &&

((k = e.key) == key || (key != null && key.equals(k))))

break;

p = e;

}

}

if (e != null) { // existing mapping for key

V oldValue = e.value;

if (!onlyIfAbsent || oldValue == null)

举报

相关推荐

0 条评论