HashMap优点总结:
- 可存储不同类型的数据:使用泛型来定义键和值的类型,兼容所有数据类型
- 高效的查找和插入操作:通过key的hash映射,实现快速的查找和插入操作。时间复杂度基本为O(1)
- 灵活的容量调整:可根据数据量增长自行动态扩容。当容量过大时,HashMap会自动进行缩容,从而提高空间利用率
- 节点转换:当链表中的节点个数超过8个时,结构会转换为红黑树,以减少平均查找时间,提高插入和删除操作的性能。
- 可存储大量数据:HashMap的存储空间只受限于计算机的内存大小。
- 支持快速迭代:HashMap提供了快速的迭代方法,可以方便地遍历所有的键值对。
- 优秀的算法1:表容量保证是2的幂数,容量-1做与运算的散列效果更好,同时便于计算扩容后元素位置,可以提高扩容效率
- 优秀的算法2:高效的容量值优化算法,借用位运算高效运算。输入容量-1,之后将后面的每一个二进制位都转为1,最后值+1,保证最终结果一定是大于等于当前值的最小2的幂
- 优秀的算法3:key的hash运算,key的hashcode值向右位运算,纳入高位影响,更好的实现扩散效果,同时减少hash碰撞
- 迭代器快速失败:HashMap的迭代器使用快速失败机制,通过modCount属性监测并发修改,即在迭代时如果有其他线程并发地修改了HashMap的结构(例如添加或删除键值对),那么迭代器会立即抛出ConcurrentModificationException异常,避免出现数据不一致的情况。
好的设计思想:根据不同数据量,灵活切换多样合适的数据结构,如Redis的数据结构中也有体现
源码解析:
HashMap类
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
泛型容纳所有类型的key,value包括null
属性解析
/**
* 版本序列号,可保证当类增减属性后,依旧可以反序列化成功
* 位运算,提醒大家hash表大小一定是2的幂
*/
private static final long serialVersionUID = 362498820763181265L;
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
/**
* 总数量,默认最大容量;最大容量一定是2的幂数
* 好处1:2的幂值-1做与运算散列效果更好
* 好处2:便于计算扩容后元素位置可以提高扩容效率
*/
static final int MAXIMUM_CAPACITY = 1 << 30;
/**
* 构造方法不指定时的默认负载因子
* 达到总容量的75% 即resize扩容
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
* 链表长度至少达到8 结构才能转为红黑树
*/
static final int TREEIFY_THRESHOLD = 8;
/**
* 树的节点数减到6 退化为链表
*/
static final int UNTREEIFY_THRESHOLD = 6;
/**
* 数组的长度至少达到64,链表才能转为红黑树
*/
static final int MIN_TREEIFY_CAPACITY = 64;
Node解析
Node存储实际key,value数据的结构,TreeNode的父类
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;
}
}
key的hash计算方式解析
static final int hash(Object key) {
int h;
// 向右位运算,纳入高位影响,更好的实现扩散效果,同时减少hash碰撞,^运算结果不同为1相同为0
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
tableSizeFor表扩容容量值优化方法解析
为保证key更好的分散效果及更佳的扩容效率,保证最大容量始终是2的幂
/**
* 将输入容量规范化为2的幂
* 算法逻辑:位运算效率更高。输入容量-1,之后将后面的每一个二进制位都转为1,最后值+1,保证最终结果一定是大于等于当前值的最小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;
}
Fields 其他属性解析
/* ---------------- Fields -------------- */
/**
* 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.)
* transient 修饰,序列化会忽略此属性
*/
transient Node<K,V>[] table;
/**
* Holds cached entrySet(). Note that AbstractMap fields are used
* for keySet() and values().
*/
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中用于记录结构修改次数,以支持Fail-Fast机制并防止并发修改导致的不一致操作。
*/
transient int modCount;
/**
* The next size value at which to resize (capacity * load factor).
*
* @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;
put方法解析
第一次初始化hash表是在第一次调用put的时候
插入,更新,获取都是大体思路相同
- 判断表是否为null或长度length=0
- 定位到hash桶中key的位置
- 判断头结点是否存在并进行处理
- 判断为树结构并进行处理
- 判断为链表结构并进行处理
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>.)
*/
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
/**
* Implements Map.put and related methods
*
* @param hash hash for key
* @param key the key
* @param value the value to put
* @param onlyIfAbsent if true, don't change existing value
* @param evict if false, the table is in creation mode.
* @return previous value, or null if none
*/
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;
// hash桶定位计算方法(n - 1) & hash。模运算也可以做到,但是没有位运算效率高
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 != null && 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)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
标签:hash,HashMap,int,value,源码,key,return,null,优点
From: https://blog.csdn.net/qq_44845473/article/details/141233333