跟我一起阅读Java源代码之HashMap
Posted on跟我一起阅读Java源代码之HashMap
最近闲的很,想和大家一起学习并讨论下Java的一些源代码以及其实现的数据结构,
不是什么高水平的东西,有兴趣的随便看看
为什么要用Map,以HashMap为例
很多时候我们有这样的需求,我们需要将数据成键值对的方式存储起来,根据key来获取value(value可以是简单值,也可以是自定义对象)
当然用对象数组也能实现这个目的,查找时可以遍历数组,比较关键字来获取对应的value
从性能上来讲,遍历大数组会消耗性能
从API易用性来讲,需要自己实现查找的逻辑
所以用HashMap是必要的
- HashMap的数据结构是怎么样的
我一直对HashMap的内部结构很好奇,看了源码之后发现他是用散列实现的,即基于hashcode
大体思想是这样的
1. 首先建立一个数组用来存取数据,假设我们定义一个Object[] table用来存取map的value
这个很容易理解,key存在哪里呢?暂时我不想存储key
2. 获得key的hashcode经过一定算法转成一个整数
index,这个index的取值范围必须是0=<index<table.length,然后我将其作为数组元素的下标
比如执行这样的操作:table[index] = value;
这样存储的问题解决了
3. 如何通过key去获取这个value呢
这个太简单了,首先获取key的hashcode,然后通过刚才一样的算法得出元素下标index
然后value = table[index]
简单的HashTable实现如下
- public class SimpleHashMap {
- private Object[] table;
- public SimpleHashMap() {
- table = new Object[10];
- }
- public Object get(Object key) {
- int index = indexFor(hash(key.hashCode()), 10);
- return table[index];
- }
- public void put(Object key, Object value) {
- int index = indexFor(hash(key.hashCode()), 10);
- table[index] = value;
- }
- ///
- /* 通过hash code 和table的length得到对应的数组下标
- /*
- /* @param h
- /* @param length
- /* @return
- /*/
- static int indexFor(int h, int length) {
- return h & (length - 1);
- }
- ///
- /* 通过一定算法计算出新的hash值
- /*
- /* @param h
- /* @return
- /*/
- static int hash(int h) {
- h ^= (h >>> 20) ^ (h >>> 12);
- return h ^ (h >>> 7) ^ (h >>> 4);
- }
- public static void main(String[] args){
- SimpleHashMap hashMap = new SimpleHashMap();
- hashMap.put("key", "value");
- System.out.println(hashMap.get("key"));
- }
- }
这个简单的例子大概描述了散列实现hashmap的过程
但是还很不成熟,我发现至少存在以下两个问题
hashmap的size是固定的
如果不同的key通过hashcode得出的index相同呢,这样的情况是存在的,如何解决? 来源: [http://tangyanbo.iteye.com/blog/1755636](http://tangyanbo.iteye.com/blog/1755636) 由于table的大小是有限的,而key的集合范围是无限大的,所以寄希望于hashcode散落,肯定会出现多个key散落在同一个数组下标下面,
因此我们要引入另外一个概念,将key和value同时存入table[index]中,即将key和value构成一个对象放在table[index],而且可能存放多个,他们的key对应的index相同,但是key本身不同
现在我们就该讨论以什么样的方式存储这些散落在同一个数组下标的元素
可以考虑数组?
也可以考虑链表存储
源码里面是用链表存储的,其实我也没明白这两种方式在这里有什么区别
,感觉无论在检索和存储上都是差不多的效率,
检索都是需要遍历的方式,而存储也可以是顺序的
那这个问题留给大家吧。
我们来实现链式存储的方式,首先定义一个链表数据结构Entry:
- public class Entry
{ - //存储key
- final K key;
- //存储value
- V value;
- //存储指向下一个节点的指针
- Entry
next; - //存储key映射的hash
- final int hash;
- }
新的EntryHashMap实现方式
- public class EntryHashMap
{ - transient Entry[] table;
- transient int size;
- public V put(K key, V value) {
- //计算出新的hash
- int hash = hash(key.hashCode());
- //计算出数组小标i
- int i = indexFor(hash, table.length);
- //遍历table[i],如果table[i]没有与新加入的key相等的,则新加入
- //一个value到table[i]中的entry,否则将新的value覆盖旧的value并返回旧的value
- for (Entry
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;
- return oldValue;
- }
- }
- addEntry(hash, key, value, i);
- return null;
- }
- public void addEntry(int hash, K key, V value, int bucketIndex) {
- Entry
e = table[bucketIndex]; - //将新的元素插入链表前端
- table[bucketIndex] = new Entry<>(hash, key, value, e);
- size++;
- }
- ///
- /* 通过hash code 和table的length得到对应的数组下标
- /*
- /* @param h
- /* @param length
- /* @return
- /*/
- static int indexFor(int h, int length) {
- return h & (length - 1);
- }
- ///
- /* 通过一定算法计算出新的hash值
- /*
- /* @param h
- /* @return
- /*/
- static int hash(int h) {
- h ^= (h >>> 20) ^ (h >>> 12);
- return h ^ (h >>> 7) ^ (h >>> 4);
- }
- }
来源: [http://tangyanbo.iteye.com/blog/1756074](http://tangyanbo.iteye.com/blog/1756074) 为什么要用链表而不是数组
链表的作用有如下两点好处
remove操作时效率高,只维护指针的变化即可,无需进行移位操作
重新散列时,原来散落在同一个槽中的元素可能会被散落在不同的地方,对于数组需要进行移位操作,而链表只需维护指针
今天研究下数组长度不够时的处理办法
table为散列数组
首先定义一个不可修改的静态变量存储table的初始大小 DEFAULT_INITIAL_CAPACITY
定义一个全局变量存储table的实际元素长度,size
定义一个全局变量存储临界点,即元素的size>=threshold这个临界点时,扩大table的容量
因为index是根据hash和table的长度计算得到的,所以还需要重新对所有元素进行散列
实现如下:
- package sourcecoderead.collection.map;
- public class EntryHashMap
{ - /// 初始容量 /*/
- static final int DEFAULT_INITIAL_CAPACITY = 16;
- static final float DEFAULT_LOAD_FACTOR = 0.75f;
- /// 下次扩容的临界值 /*/
- int threshold;
- transient int size;
- final float loadFactor;
- transient Entry[] table;
- public EntryHashMap() {
- this.loadFactor = DEFAULT_LOAD_FACTOR;
- threshold = (int) (DEFAULT_INITIAL_CAPACITY /* DEFAULT_LOAD_FACTOR);
- table = new Entry[DEFAULT_INITIAL_CAPACITY];
- }
- public V put(K key, V value) {
- // 计算出新的hash
- int hash = hash(key.hashCode());
- // 计算出数组小标i
- int i = indexFor(hash, table.length);
- // 遍历table[i],如果table[i]没有与新加入的key相等的,则新加入
- // 一个value到table[i]中的entry,否则将新的value覆盖旧的value并返回旧的value
- for (Entry
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;
- return oldValue;
- }
- }
- addEntry(hash, key, value, i);
- return null;
- }
- public V get(K key) {
- // 计算出新的hash
- int hash = hash(key.hashCode());
- // 计算出数组小标i
- int i = indexFor(hash, table.length);
- for (Entry
e = table[i]; e != null; e = e.next) { - Object k;
- if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
- return e.value;
- }
- }
- return null;
- }
- private void addEntry(int hash, K key, V value, int bucketIndex) {
- Entry
e = table[bucketIndex]; - // 将新的元素插入链表前端
- table[bucketIndex] = new Entry<>(hash, key, value, e);
- if (size++ >= threshold)
- resize(2 /* table.length);
- }
- void resize(int newCapacity) {
- Entry[] oldTable = table;
- int oldCapacity = oldTable.length;
- Entry[] newTable = new Entry[newCapacity];
- transfer(newTable);
- table = newTable;
- threshold = (int) (newCapacity /* loadFactor);
- }
- void transfer(Entry[] newTable) {
- Entry[] src = table;
- int newCapacity = newTable.length;
- for (int j = 0; j < src.length; j++) {
- Entry
e = src[j]; - if (e != null) {
- src[j] = null;
- do {
- Entry
next = e.next; - int i = indexFor(e.hash, newCapacity);
- e.next = newTable[i];
- newTable[i] = e;
- e = next;
- } while (e != null);
- }
- }
- }
- ///
- /* 通过hash code 和table的length得到对应的数组下标
- /*
- /* @param h
- /* @param length
- /* @return
- /*/
- static int indexFor(int h, int length) {
- return h & (length - 1);
- }
- ///
- /* 通过一定算法计算出新的hash值
- /*
- /* @param h
- /* @return
- /*/
- static int hash(int h) {
- h ^= (h >>> 20) ^ (h >>> 12);
- return h ^ (h >>> 7) ^ (h >>> 4);
- }
- public static void main(String[] args) {
- EntryHashMap
hashMap = new EntryHashMap (); - hashMap.put("key", "value");
- System.out.println(hashMap.get("key"));
- }
- }
来源: [http://tangyanbo.iteye.com/blog/1756536](http://tangyanbo.iteye.com/blog/1756536)