🍁 作者:知识浅谈,CSDN 博客专家,阿里云签约博主,InfoQ 签约博主,华为云云享专家
📌 擅长领域:全栈工程师、爬虫、ACM 算法
💒 公众号:知识浅谈
以下的HashMap版本都是基于JDK1.8的版本进行源码查看的🤞这次都给他拿下🤞
正菜来了⛳⛳⛳
🎈HashMap 中的方法和类
🍮静态内部类 Node
首先我们知道 HashMap 中的存储形式是按照数组+链表+红黑树,在数组和链表中存储元素的方式是使用 Node 节点的形式存储的,具体的 Node 节点的实现形式如下。
static class Node<K,V> implements Map.Entry<K,V> { 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; }}
复制代码
从以上的 Node 内部类中可以看出存储的 key 值,hash 值,value 值,next 值,以及重写的 equals 方法,hashCode 方法等。
🍮int hash(Object key)
含义:这个 HashMap 中的 hashCode 方法可有门道了,从下边的函数中可以看出先求出 key 的 hash 值,再把哈希值右移 16 位和原来的 h 取交集即可。
static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);}
复制代码
官方的解释如下:计算 key.hashCode() 并将哈希的较高位传播(XOR)到较低位。由于该表使用二次幂掩码,因此仅在当前掩码之上位变化的散列集将始终发生冲突。 (已知的例子是在小表中保存连续整数的 Float 键集。)因此,我们应用了一种变换,将高位的影响向下传播。在位扩展的速度、实用性和质量之间存在折衷。因为许多常见的散列集已经合理分布(所以不要从传播中受益),并且因为我们使用树来处理 bin 中的大量冲突,我们只是以最便宜的方式对一些移位的位进行异或,以减少系统损失,以及合并最高位的影响,否则由于表边界,这些最高位将永远不会用于索引计算。
🍮comparableClassFor(Object x)
含义: 如果 x 的 Class 是“class C implements Comparable”的形式,则返回 x 的 Class,否则返回 null。从下边的函数中可以看出先判度 x 是不是 String.class,如果是的话就返回 c,如果不是获取 c 实现的接口中有没有 Comparable,有的话返回 c,如果以上两者都不能满足,直接返回 null。
static Class<?> comparableClassFor(Object x) { if (x instanceof Comparable) { Class<?> c; Type[] ts, as; Type t; ParameterizedType p; if ((c = x.getClass()) == String.class) // bypass checks return c; if ((ts = c.getGenericInterfaces()) != null) { for (int i = 0; i < ts.length; ++i) { if (((t = ts[i]) instanceof ParameterizedType) && ((p = (ParameterizedType)t).getRawType() == Comparable.class) && (as = p.getActualTypeArguments()) != null && as.length == 1 && as[0] == c) // type arg is c return c; } } } return null;}
复制代码
🍮int tableSizeFor(int cap)
含义:返回给定目标容量的 2 次方。
static final int tableSizeFor(int cap) { int n = cap - 1; n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;}
复制代码
🍮HashMap(int initialCapacity, float loadFactor)
含义:构造一个具有指定初始容量和负载因子的空 HashMap,如果指定的初始容量小于 0 的话,抛出异常,如果大于 MAXIMUM_CAPACITY 的话,就把 initialCapacity 设置为 initialCapacity ,loadFactor 设置成自己传入的参数。
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); }
复制代码
🍚总结
以上是关于 HashMap 的源码解析的部分函数,希望有所帮助,Written By 知识浅谈
评论