HashMap工作原理
来源:互联网 发布:淘宝怎么显示佣金 编辑:程序博客网 时间:2024/06/08 01:21
首先看源码
/** * An empty table instance to share when the table is not inflated. */ static final Entry<?,?>[] EMPTY_TABLE = {}; /** * The table, resized as necessary. Length MUST Always be a power of two. */transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
HashMap其实也是一个线性的数组加链表实现的,所以可以理解为其存储数据的容器就是一个线性数组。
看下图
一个数组,每一个数组单元会有一个链表
Entry作为一个内部类,看源码就会发现他有next属性,说明有链表的性质,指向下一个。
static class Entry<K,V> implements Map.Entry<K,V> { final K key; V value; Entry<K,V> next; int hash; /** * Creates new entry. */ Entry(int h, K k, V v, Entry<K,V> n) { value = v; next = n; key = k; hash = h; } }
在上面代码中,Entry做一个一个内部类,有主要next,key,value,hash四个属性
再上图中,在每一个数组单元里其实都是一又一个链表组成的
首先要获取table的长度,hash(key) 获得一个int值,然后用去取余table.lenth
上图数组的长度是8 ,比如A元素的的KEY哈希值为21 ,那么21%(8-1)=0 那么Entry[0] = A;
当第二个元素B的KEY经过hash()后为14,那么14%(8-1)=0,这个时候Entry[0]=B,则next=A。
如此反复,后进来会持有先进来的next。
接着看put()代码
首先判断是table是否为空,如果为空的话进入inflateTable(threshold);
意思就是给table充气,然后会给table分配一个长度空间。然后来判断Key是不是空,如果是空的的话,会首先放到数组的第一个位置Entry[0].接下来就和上面说的差不多了,开始拿hash(key)%table.lenth,源码是return h & (length-1);
然后进行循环判断是否有重复的键,如果有将之前的覆盖。
public V put(K key, V value) { if (table == EMPTY_TABLE) { inflateTable(threshold); } if (key == null) return putForNullKey(value); int hash = hash(key); int i = indexFor(hash, table.length); for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(hash, key, value, i); return null; }
Ps:这也说明Set为什么会去重,拿HashSet来讲,add的时候,就是把元素传到了HashMap的put方法中,元素充当了HashMap的键。
接着看get方法
public V get(Object key) { if (key == null) return getForNullKey(); Entry<K,V> entry = getEntry(key); return null == entry ? null : entry.getValue(); }
如果key为null的话,那么直接是在数组的第0个位置去寻找,通过next再去寻找链表
for (Entry<K,V> e = table[0]; e != null; e = e.next) { if (e.key == null) return e.value; }
如果不为null的话,走getEntry();一下是getEntry代码
final Entry<K,V> getEntry(Object key) { if (size == 0) { return null; } int hash = (key == null) ? 0 : hash(key); for (Entry<K,V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } return null; }
先算出Hash,计算出在数组的哪个位置,然后开始for循环next寻找对应key的entry
- HashMap的工作原理
- HashMap的工作原理
- HashMap的工作原理
- HashMap的工作原理
- HashMap的工作原理
- HashMap的工作原理
- HashMap的工作原理
- HashMap的工作原理
- hashmap工作原理
- HashMap的工作原理
- HashMap的工作原理
- HashMap的工作原理
- HashMap工作原理
- HashMap的工作原理
- hashmap工作原理
- HashMap的工作原理
- HashMap的工作原理
- HashMap的工作原理
- 测试用例设计方法
- Android使double保留两位小数的多方法 java保留两位小数
- DPDK之(七)—— support for vhost-user学习笔记
- 2017 google host文件
- 用phpcms将静态页面制作成企业网站标签总结
- HashMap工作原理
- Controlling Constraints, Identities, and Triggers with NOT FOR REPLICATION
- eclipse中tomcat server at localhost配置
- 分布式架构学习之:030--Keepalived+Nginx实现高可用Web负载均衡
- DPDK之(八)——vhost库
- torch7 require"cutorch" 出错解决办法
- C#类(Class)
- 统计学习方法 第4章 朴素贝叶斯法 习题答案
- Android buildToolsVersion