“在介绍HashMap之前,为了方便更清楚地理解源码,先大致说说HashMap的实现原理,
HashMap 是基于数组 + 链表实现的, 首先HashMap就是一个大数组,在这个数组中,通过hash值去寻对应位置的元素, 如果遇到多个元素的hash值一样,那么怎么保存,这就引入了链表,在同一个hash的位置,保存多个元素(通过链表关联起来)。HashMap 所实现的基于<key, value>的键值对形式,是由其内部内Entry实现。”
Entry,是一个静态类,实现Map.Entry< K ,V>,通过他可以构成一个单向链表,只是一个内部类。
HashMap类继承了AbstractMap父类,实现Map,Cloneable, Serializble接口。
public class HashMAp<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
- serialVersionUID叫序列化ID 先摆着:
java对象序列化的意思就是将对象的状态转化成字节流,以后可以通过这些值再生成相同状态的对象。对象序列化是对象持久化的一种实现方法,它是将对象的属性和方法转化为一种序列化的形式用于存储和传输。反序列化就是根据这些保存的信息重建对象的过程。
序列化:将java对象转化为字节序列的过程。
反序列化:将字节序列转化为java对象的过程。 - 负载因子:
会影响HashMap性能
首先回忆HashMap的数据结构,- 我们都知道有序数组存储数据,对数据的索引效率都很高,但是插入和删除就会有性能瓶颈(回忆ArrayList)
- 链表存储数据,要一次比较元素来检索出数据,所以索引效率低,但是插入和删除效率高(回忆LinkedList),
两者取长补短就产生了哈希散列这种存储方式,也就是HashMap的存储逻辑.
而负载因子表示一个散列表的空间的使用程度,有这样一个公式:initailCapacity*loadFactor=HashMap的容量。
所以负载因子越大则散列表的装填程度越高,也就是能容纳更多的元素,元素多了,链表大了,所以此时索引效率就会降低。
反之,负载因子越小则链表中的数据量就越稀疏,此时会对空间造成浪费,但是此时索引效率高。
官方操作一波0.75f,咱也不知道为啥哈哈哈,效率高呗。
JDK1.8 开始HashMap的实现是 数组+链表+红黑树
//序列化ID
private static final long serialVersionUID = 362498820763181265L;
// 初始容量16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
//最大容量2^30
static final int MAXIMUM_CAPACITY = 1 << 30;
//默认负载因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//当链表达到8时转化为红黑树
static final int TREEIFY_THRESHOLD = 8;
//当红黑树节点数小于6时,转化为链表
static final int UNTREEIFY_THRESHOLD = 6;
/**
* 这个字段决定了当hash表的至少大小为多少时,链表才能进行树化。这个设计时合理的,
* 因为当hash表的大小很小时,这时候表所需的空间还不多,可以牺牲空间减少时间,所以这个情况下
* 当存储的节点过多时,最好的办法是调整表的大小,使其增大,而不是将链表树化。
*/
static final int MIN_TREEIFY_CAPACITY = 64;
下面介绍键值在HashMap中的存储形式。
static class Node<K,V> implements Map.Entry<K,V> {
final int hash; //hash的value
final K key; //key
V value; //value 的 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;
}
//Entry的get方法、toString方法
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
计算hashCode中key和value异或值,将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;
}
判断地址值,判断o是否为Map.Entry的实例,判断类型和键值。
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;
}
hash(Object key)
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
先获取到key的hashCode,然后进行移位再进行异或运算,复杂操作原因为了减少hash冲突。
“ 如果恶意程序知道我们用的是Hash算法,则在纯链表情况下,它能够发送大量请求导致哈希碰撞,然后不停访问这些key导致HashMap忙于进行线性查找,最终陷入瘫痪,即形成了拒绝服务攻击(DoS)。
关于Java 8中的hash函数,原理和Java 7中基本类似。Java 8中这一步做了优化,只做一次16位右位移异或混合,而不是四次,但原理是不变的。”
comparableClassFor(Object x)
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;
}
comparableClassFor(Object x)方法,当x的类型为X,且X直接实现了Comparable接口(比较类型必须为X类本身)时,返回x的运行时类型;否则返回null。
compareComparables(Class<?> kc, Object k, Object x)
@SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
static int compareComparables(Class<?> kc, Object k, Object x) {
return (x == null || x.getClass() != kc ? 0 :
((Comparable)k).compareTo(x));
}
如果x的类型为kc,则返回k.compareTo(x),否则返回0.
tableSizeFor(int cap)
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1; // 将n和n右移1位的值进行或运算,将结果赋值给n
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
返回大于等于cap的最小的二次幂数值。
/**
存储键值对的数组,一般是2的幂
* The table, initialized on first use, and resized as
* necessary. When allocated, length is always a power of two.
* (We also tolerate length zero in some operations to allow
* bootstrapping mechanics that are currently not needed.)
* 存储键值对的数组,一般是2的幂
*/
transient Node<K,V>[] table;
/**
* Holds cached entrySet(). Note that AbstractMap fields are used
* for keySet() and values().
* 键值对缓存,它们的映射关系集合保存在entrySet中。
* 即使Key在外部修改导致hashCode变化,缓存中还可以找到映射关系
*/
transient Set<Map.Entry<K,V>> entrySet;
/**
* The number of key-value mappings contained in this map.
* 键值对的实际个数
*/
transient int size;
/**
* The number of times this HashMap has been structurally modified
* Structural modifications are those that change the number of mappings in
* the HashMap or otherwise modify its internal structure (e.g.,
* rehash). This field is used to make iterators on Collection-views of
* the HashMap fail-fast. (See ConcurrentModificationException).
* 记录HashMap被修改结构的次数。
* 修改包括改变键值对的个数或者修改内部结构,比如rehash
* 这个域被用作HashMap的迭代器的fail-fast机制中
* (参考ConcurrentModificationException)
*/
transient int modCount;
/**
* The next size value at which to resize (capacity * load factor).
* 扩容的临界值,通过capacity * load factor可以计算出来。超过这个值HashMap将进行扩容
* @serial
*/
// (The javadoc description is true upon serialization.
// Additionally, if the table array has not been allocated, this
// field holds the initial array capacity, or zero signifying
// DEFAULT_INITIAL_CAPACITY.)
int threshold;
/**
* The load factor for the hash table.
*负载因子
* @serial
*/
final float loadFactor;
public HashMap(int initialCapacity, float loadFactor)
/**
* Constructs an empty <tt>HashMap</tt> with the specified initial
* capacity and load factor.
*
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
* @throws IllegalArgumentException if the initial capacity is negative
* or the load factor is nonpositive
*/
/**
* 使用指定的初始化容量initial capacity 和负载因子load factor构造一个空HashMap
*
* @param initialCapacity 初始化容量
* @param loadFactor 负载因子
* @throws IllegalArgumentException 如果指定的初始化容量为负数或者加载因子为非正数。
*/
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)
/**
* Constructs an empty <tt>HashMap</tt> with the specified initial
* capacity and the default load factor (0.75).
*
* @param initialCapacity the initial capacity.
* @throws IllegalArgumentException if the initial capacity is negative.
*/
/**
* 使用指定的初始化容量initial capacity和默认负载因子DEFAULT_LOAD_FACTOR(0.75)构造一个空HashMap
*
* @param initialCapacity 初始化容量
* @throws IllegalArgumentException 如果指定的初始化容量为负数
*/
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
public HashMap()
/**
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
*/
/**
* 使用指定的初始化容量(16)和默认负载因子DEFAULT_LOAD_FACTOR(0.75)构造一个空HashMap
*/
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
public HashMap(Map<? extends K, ? extends V> m)
/**
* Constructs a new <tt>HashMap</tt> with the same mappings as the
* specified <tt>Map</tt>. The <tt>HashMap</tt> is created with
* default load factor (0.75) and an initial capacity sufficient to
* hold the mappings in the specified <tt>Map</tt>.
*
* @param m the map whose mappings are to be placed in this map
* @throws NullPointerException if the specified map is null
* *使用与
*指定的<tt>map.<tt>创建“hashmap”时
*默认荷载系数(0.75)和初始承载力足以
*将映射保存在指定的<tt>映射中。
*
*@param m要将其映射放置在此映射中的映射
*@如果指定的映射为空,则引发NullPointerException
*/
public HashMap(Map<? extends K, ? extends V> m) {
this.loadFactor = DEFAULT_LOAD_FACTOR;
putMapEntries(m, false);
整个夜,疯狂挥霍。
final void final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) (Map<? extends K, ? extends V> m, boolean evict)
//将一个map即m放入当前实例的table中
final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
int s = m.size();
if (s > 0) {
//如果table没初始化,ft = 一个值
//t = ft 与 最大mum容量比较 赋值
//t > 阈值条件,赋值改变threshold
if (tablet == null) { // pre-size
float ft = ((float)s / loadFactor) + 1.0F;
int t = ((ft < (float)MAXIMUM_CAPACITY) ?
(int)ft : MAXIMUM_CAPACITY);
if (t > threshold)
threshold = tableSizeFor(t);
}
else if (s > threshold)
resize();
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
K key = e.getKey();
V value = e.getValue();
putVal(hash(key), key, value, false, evict);
}
}
}
啊mdzz… 搞来搞去就是一顿操作改了threshold的值为tableSizeFor((map大小 / loadFactor) + 1.0F)
public int size()
/**
* Returns the number of key-value mappings in this map.
*
* @return the number of key-value mappings in this map
*/
/**
*返回此映射中的键值映射数
*@返回此映射中的键值映射数
*/
public int size() {
return size;
}
public boolean isEmpty()
/**
* Returns <tt>true</tt> if this map contains no key-value mappings.
*
* @return <tt>true</tt> if this map contains no key-value mappings
*/
/**
*如果此映射不包含键值映射,则返回<tt>true。
*@如果此映射不包含键值映射,则返回<tt>true
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
*
* <p>More formally, if this map contains a mapping from a key
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
* key.equals(k))}, then this method returns {@code v}; otherwise
* it returns {@code null}. (There can be at most one such mapping.)
*
* <p>A return value of {@code null} does not <i>necessarily</i>
* indicate that the map contains no mapping for the key; it's also
* possible that the map explicitly maps the key to {@code null}.
* The {@link #containsKey containsKey} operation may be used to
* distinguish these two cases.
*
* @see #put(Object, Object)
*/
/**
*返回指定键映射到的值,
*或者@code NULL如果此映射不包含键的映射。
*<p>更正式地说,如果此映射包含来自键的映射
*@code k到值@code v这样@code(key==null?K==空:
*key.equals(k)),则此方法返回@code v,否则返回
*返回@code空。(最多可以有一个这样的映射。)
*<p>返回值@code null不一定<i>
*指示映射不包含键的映射;它还
*可能映射显式地将键映射到@code null。
*@link containskey containskey操作可用于
*区分这两种情况。
* @see #put(Object, Object)
*/
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node<K,V> getNode(int hash, Object key)
/**
* Implements Map.get and related methods.
*
* @param hash hash for key
* @param key the key
* @return the node, or null if none
*/
/**
* 根据key的哈希值和key获取对应的节点
*
* @param hash 指定参数key的哈希值
* @param key 指定参数key
* @return 返回node,如果没有则返回null
*/
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
//表不为空、长度不为零且key对应value不空
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
//如果桶中的第一个节点得哈希值就和指定参数hash对应
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)
//当前桶用红黑树,调用红黑树get方法获取节点
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);
}
}
//若为空,返回null
return null;
}
根据key的哈希值获取key对应节点。
public boolean containsKey(Object key)
// 调用getNode方法来获取键值对,如果没有找到返回false,找到了就返回ture
public boolean containsKey(Object key) {
return getNode(hash(key), key) != null;
}
- public V put(K key, V value)
- final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict)
- final Node<K,V>[] resize()
put方法首先检查HashMap是否为空,为空执行resize初始化map,若非空,计算tab数组下标[(n - 1) & hash],判断数组对象是否为空,为空时新建一个node节点。
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;
//检查hashmap是否为空,为空的话执行resize,相当于初始化一个map
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//非空时,计算tab数组下标[(n - 1) & hash],判断数组对象是否为空,为空时新建一个node节点。
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
//数组对象非空,tab[i]非空,
//判断该节点的key与即将put的key值是否相同,相同的话先讲tab[i]对应的node存储起来。
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//判断tab[i]是否为红黑树对象,若tab节点为红黑树,则执行一次树对象put操作。
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
//处理tab[i]节点为链表对象,通过一个计数器binCount统计链表长度。如果tab[i]对象p的next为null,则链表到头了,这个时候新建一个node<key,value>节点为p.next。
//如果链表长度计数器binCount>7即TREEIFY_THRESHOLD - 1即8-1,即链表长度大于8时,则进行红黑树转换。如果不满足转换条件,链表种插入新节点完毕,无需其他操作
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;
//针对存在相同key的节点,执行value覆盖,并返回旧值。
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
//若tab大小超过阈值(容量*负载因子),执行resize扩容操作,返回null。
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) {
// 老容量超过最大容量,不进行扩容
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
// 扩容两倍
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
// 新建扩容数组
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
if (oldTab != null) {
// 遍历老数组
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
// 老数组子节点为null,通过e.hash & (newCap - 1)获取数组下标,将节点填充到该数组对象中
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
// 节点为红黑色时
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
// 元素位置没有发生变化
// 原hash与原容量进行与运算,loHead、loTail位置不变时的头尾节点
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
// 元素位置发生变化
// hiHead、hiTail位置变化后新的头节点和尾节点
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
// 位置不变时,(e.hash & oldCap) == 0,数组当前下标指向loHead
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
// 位置变化时,数组下标变为[j + oldCap],指向头节点
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
final void treeifyBin(Node<K,V>[] tab, int hash)树化
/**
* Replaces all linked nodes in bin at index for given hash unless
* table is too small, in which case resizes instead.
*/
/**
* Replaces all linked nodes in bin at index for given hash unless
* table is too small, in which case resizes instead.
*/
/**
*替换给定哈希的索引处bin中的所有链接节点,除非
*表太小,在这种情况下会调整大小。
*/
final void treeifyBin(Node<K,V>[] tab, int hash) {
int n, index; Node<K,V> e;
//如果元素数组长度为空或者小于MIN_TREEIFY_CAPACITY,执行resize操作
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
resize();
//数组长度与hash值位运算,得到链表的首节点,hd是树首节点,tl是树尾结点
else if ((e = tab[index = (n - 1) & hash]) != null) {
TreeNode<K,V> hd = null, tl = null;
do {
//节点变成树节点
TreeNode<K,V> p = replacementTreeNode(e, null);
//如果尾结点为空,赋值给首节点,树的根结点
//如果尾结点不为空,则是双向链表结构构成
if (tl == null)
hd = p;
else {
//prev指向前一个节点尾结点
p.prev = tl;
//next指向当前节点
tl.next = p;
}
//把当前节点设置为尾结点
tl = p;
} while ((e = e.next) != null);
if ((tab[index] = hd) != null)
hd.treeify(tab);
//转换成红黑树
}
}
public void putAll(Map<? extends K, ? extends V> m)
/**
* Copies all of the mappings from the specified map to this map.
* These mappings will replace any mappings that this map had for
* any of the keys currently in the specified map.
*
* @param m mappings to be stored in this map
* @throws NullPointerException if the specified map is null
*/
public void putAll(Map<? extends K, ? extends V> m) {
//调用putMapEntries
putMapEntries(m, true);
}