HashMap的源码分析
程序员文章站
2022-06-04 19:31:43
...
存储结构:用数组来存桶的第一个节点。每个桶都是一个链表。里面存 hashcode & table.length 相同的那些Entry
构造函数:
loadFactor指的是重新初始化的一个伐值。比如说0.75.不理解设置这个伐值的好处是什么。实际初始化的table大小是大于initialCapacity的2的阶乘。这样做的好处是可以充分的利用table的空间,因为在查找桶的index的时候是通过 hashcode & table.length 来实现的。举个例子当length 为7时,那么当hashcode后三位为010的entry只能存到0桶里面。而本来010应该存在2桶里面。
put方法:先找到桶,看对应的Key是不是存在,如果存在,那么覆盖原来的value值。当size > threshold时会执行resize.在transfer 方法里面会重新计算原来table中entry在新table中的桶的index,并将它分配到新桶中
get key方法:通过key的hashcode先找到桶的index,然后遍历链表,看是否存在
hashmap 中有个成员变量 modCount,用来记录被更改的次数,这个变量的作用是当遍历hashmap的时候,如果modCount发生变化,就说明hashmap变换了,那就会抛出ConcurrentModificationException错误。代码如下
总结:hashmap不是线程安全的,可以存储key不同,value相同的值。
transient Entry[] table;
构造函数:
loadFactor指的是重新初始化的一个伐值。比如说0.75.不理解设置这个伐值的好处是什么。实际初始化的table大小是大于initialCapacity的2的阶乘。这样做的好处是可以充分的利用table的空间,因为在查找桶的index的时候是通过 hashcode & table.length 来实现的。举个例子当length 为7时,那么当hashcode后三位为010的entry只能存到0桶里面。而本来010应该存在2桶里面。
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);
// Find a power of 2 >= initialCapacity
int capacity = 1;
while (capacity < initialCapacity)
capacity <<= 1;
this.loadFactor = loadFactor;
threshold = (int)(capacity * loadFactor);
table = new Entry[capacity];
init();
}
put方法:先找到桶,看对应的Key是不是存在,如果存在,那么覆盖原来的value值。当size > threshold时会执行resize.在transfer 方法里面会重新计算原来table中entry在新table中的桶的index,并将它分配到新桶中
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
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;
}
void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}
void resize(int newCapacity) {
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return;
}
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<K,V> e = src[j];
if (e != null) {
src[j] = null;
do {
Entry<K,V> next = e.next;
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
} while (e != null);
}
}
}
get key方法:通过key的hashcode先找到桶的index,然后遍历链表,看是否存在
public V get(Object key) {
if (key == null)
return getForNullKey();
int hash = hash(key.hashCode());
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.equals(k)))
return e.value;
}
return null;
}
hashmap 中有个成员变量 modCount,用来记录被更改的次数,这个变量的作用是当遍历hashmap的时候,如果modCount发生变化,就说明hashmap变换了,那就会抛出ConcurrentModificationException错误。代码如下
private abstract class HashIterator<E> implements Iterator<E> {
Entry<K,V> next; // next entry to return
int expectedModCount; // For fast-fail
int index; // current slot
Entry<K,V> current; // current entry
HashIterator() {
expectedModCount = modCount;
if (size > 0) { // advance to first entry
Entry[] t = table;
while (index < t.length && (next = t[index++]) == null)
;
}
}
public final boolean hasNext() {
return next != null;
}
final Entry<K,V> nextEntry() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Entry<K,V> e = next;
if (e == null)
throw new NoSuchElementException();
if ((next = e.next) == null) {
Entry[] t = table;
while (index < t.length && (next = t[index++]) == null)
;
}
current = e;
return e;
}
public void remove() {
if (current == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Object k = current.key;
current = null;
HashMap.this.removeEntryForKey(k);
expectedModCount = modCount;
}
}
总结:hashmap不是线程安全的,可以存储key不同,value相同的值。
上一篇: 龙抬头在历朝历代有什么发展历史?史书又是怎么记载的?
下一篇: LinkedList