大致看了看HashMap的源码,把以前落下来的都补上。
基于JDK1.6
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
//可序列化
{
//默认的初始化容量 必须为2的整数次幂
static final int DEFAULT_INITIAL_CAPACITY = 16;
//最大的容量 2的30次幂
static final int MAXIMUM_CAPACITY = 1 << 30;
//负载因子 0.75 衡量是否需要扩容的尺度
static final float DEFAULT_LOAD_FACTOR = 0.75f;
// Entry数组 HashMap的实质就是数组+链表的结构
transient Entry[] table;
//HashMap中键值对的数量
transient int size;
//用来判断是否需要扩容 threshold = 当前容量*负载因子
int threshold;
//负载因子实际的大小
final float loadFactor;
//HashMap被修改的次数
transient volatile int modCount;
//构造方法 初始化容量和负载因子
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
//最大只能为2的30次幂
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
//找出大于"initialCapacity"的最小的2的次幂
//HashMap的容量一定要为2的次幂,并且还要大于initialCapacity,又不能太大,会浪费太多的空间
int capacity = 1;
while (capacity < initialCapacity)
capacity <<= 1;
//设置加载因子
this.loadFactor = loadFactor;
//设置扩容标准
threshold = (int)(capacity * loadFactor);
//初始化Entry数组
table = new Entry[capacity];
init();
}
//构造方法 只指定初始化容量
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
//默认的构造方法 负载因子、扩容标准、初始化容量都使用默认的 0.75,12,16
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR;
threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
table = new Entry[DEFAULT_INITIAL_CAPACITY];
init();
}
//构造方法 给定一个Map
public HashMap(Map<? extends K, ? extends V> m) {
//设定初始化容量 最少为16
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
//将m中的元素逐个添加到HashMap中
putAllForCreate(m);
}
void init() {
}
//计算hash值 空键总是被映射到0 索引为0的位置
static int hash(int h) {
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
//返回索引值 为什么初始化容量要2的次幂就在这里
static int indexFor(int h, int length) {
return h & (length-1);
}
//返回HashMap的长度
public int size() {
return size;
}
//判断是否为空
public boolean isEmpty() {
return size == 0;
}
//通过key获取value
public V get(Object key) {
if (key == null)
return getForNullKey();
//首先计算key的哈希值
int hash = hash(key.hashCode());
//先找到这个key在数组中对应的位置 将这个位置对应的entry拿到 因为每一个数组元素都是个链表 所以遍历这个链表
for (Entry<K,V> e = table[indexFor(hash, table.length)];
e != null;
e = e.next) {
Object k;
//当前Entry的哈希值必须和key的哈希值相同
//key和当前Entry的key也必须相同 就认定这个entry是我们要找的entry
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
return e.value;
}
return null;
}
//之前说key为空的entry 都在数组索引为0的位置 但如果这个位置的entry的key不为空 就返回null
private V getForNullKey() {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null)
return e.value;
}
return null;
}
public boolean containsKey(Object key) {
return getEntry(key) != null;
}
//通过key找到对应的entry
final Entry<K,V> getEntry(Object key) {
int hash = (key == null) ? 0 : 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 != null && key.equals(k))))
return e;
}
return null;
}
//添加元素 都是要先计算hash值 确定位置 遍历链表
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;
//如果key已经存在 就用新的value替换旧的value
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;
}
//将key为null的键值对放在数组索引为0的位置
private V putForNullKey(V value) {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}
//私有方法 在构造器等方法中调用 将键值对放在map中
private void putForCreate(K key, V value) {
int hash = (key == null) ? 0 : 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 != null && key.equals(k)))) {
e.value = value;
return;
}
}
createEntry(hash, key, value, i);
}
//私有方法
private void putAllForCreate(Map<? extends K, ? extends V> m) {
//利用迭代器遍历 map 获取key和value
for (Iterator<? extends Map.Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
Map.Entry<? extends K, ? extends V> e = i.next();
putForCreate(e.getKey(), e.getValue());
}
}
//扩容
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);
}
//将当前map中的内容放到扩容后的map中
void transfer(Entry[] newTable) {
Entry[] src = table;
int newCapacity = newTable.length;
for (int j = 0; j < src.length; j++) {
//遍历当前的entry数组
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;
//遍历链表 将entry重新计算哈希值 放入到新的数组中
} while (e != null);
}
}
}
//将所有元素放到HashMap中
public void putAll(Map<? extends K, ? extends V> m) {
int numKeysToBeAdded = m.size();
//判断是否为空
if (numKeysToBeAdded == 0)
return;
if (numKeysToBeAdded > threshold) {
int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
if (targetCapacity > MAXIMUM_CAPACITY)
targetCapacity = MAXIMUM_CAPACITY;
int newCapacity = table.length;
//大于'targetCapacity'的最小的2的次幂
while (newCapacity < targetCapacity)
newCapacity <<= 1;
if (newCapacity > table.length)
//扩容
resize(newCapacity);
}
//逐个添加
for (Iterator<? extends Map.Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {
Map.Entry<? extends K, ? extends V> e = i.next();
put(e.getKey(), e.getValue());
}
}
//移除
public V remove(Object key) {
Entry<K,V> e = removeEntryForKey(key);
return (e == null ? null : e.value);
}
//先遍历 找到对应的entry 然后移除 size,modCount相应减少和增加
final Entry<K,V> removeEntryForKey(Object key) {
int hash = (key == null) ? 0 : hash(key.hashCode());
int i = indexFor(hash, table.length);
Entry<K,V> prev = table[i];
Entry<K,V> e = prev;
//在链表中删除一个元素的操作 将entry的引用指向下一个就可以了
while (e != null) {
Entry<K,V> next = e.next;
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k)))) {
modCount++;
size--;
if (prev == e)
table[i] = next;
else
prev.next = next;
e.recordRemoval(this);
return e;
}
prev = e;
e = next;
}
return e;
}
//删除键值对
final Entry<K,V> removeMapping(Object o) {
if (!(o instanceof Map.Entry))
return null;
Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
Object key = entry.getKey();
int hash = (key == null) ? 0 : hash(key.hashCode());
int i = indexFor(hash, table.length);
Entry<K,V> prev = table[i];
Entry<K,V> e = prev;
while (e != null) {
Entry<K,V> next = e.next;
if (e.hash == hash && e.equals(entry)) {
modCount++;
size--;
if (prev == e)
table[i] = next;
else
prev.next = next;
e.recordRemoval(this);
return e;
}
prev = e;
e = next;
}
return e;
}
// 清空 将数组的所有元素都赋值为空
public void clear() {
modCount++;
Entry[] tab = table;
for (int i = 0; i < tab.length; i++)
tab[i] = null;
size = 0;
}
//是否包含某个value
public boolean containsValue(Object value) {
// 如果为空
if (value == null)
return containsNullValue();
// 如果不为空 分开判断可以避免空指针
Entry[] tab = table;
//遍历数组
for (int i = 0; i < tab.length ; i++)
//遍历链表
for (Entry e = tab[i] ; e != null ; e = e.next)
if (value.equals(e.value))
return true;
return false;
}
private boolean containsNullValue() {
Entry[] tab = table;
for (int i = 0; i < tab.length ; i++)
for (Entry e = tab[i] ; e != null ; e = e.next)
if (e.value == null)
return true;
return false;
}
//克隆方法
public Object clone() {
HashMap<K,V> result = null;
try {
result = (HashMap<K,V>)super.clone();
} catch (CloneNotSupportedException e) {
// assert false;
}
//先把架子搭起来
result.table = new Entry[table.length];
result.entrySet = null;
result.modCount = 0;
result.size = 0;
result.init();
//然后把元素填充进去
result.putAllForCreate(this);
return result;
}
//内部类 链表
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
final int hash;
Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
public final K getKey() {
return key;
}
public final V getValue() {
return value;
}
public final V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
// 需要判断key 和value的值都是相等 才会返回true
public final boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry e = (Map.Entry)o;
Object k1 = getKey();
Object k2 = e.getKey();
if (k1 == k2 || (k1 != null && k1.equals(k2))) {
Object v1 = getValue();
Object v2 = e.getValue();
if (v1 == v2 || (v1 != null && v1.equals(v2)))
return true;
}
return false;
}
public final int hashCode() {
return (key==null ? 0 : key.hashCode()) ^
(value==null ? 0 : value.hashCode());
}
public final String toString() {
return getKey() + "=" + getValue();
}
//当更改已存在的键值对的value的时候 调用 不做任何处理
void recordAccess(HashMap<K,V> m) {
}
//当删除链表节点的时候调用 不做任何处理
void recordRemoval(HashMap<K,V> m) {
}
}
//向链表中增加一个节点
void addEntry(int hash, K key, V value, int bucketIndex) {
//这个entry e已经指向这个链表的后续节点了
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
//判断是否需要扩容
if (size++ >= threshold)
resize(2 * table.length);
}
//确定增加节点不会超过扩容标准 才可以用这个方法
void createEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
size++;
}
private abstract class HashIterator<E> implements Iterator<E> {
//下一个元素
Entry<K,V> next;
//用于fast-fail机制
int expectedModCount;
//当前索引
int index;
//当前元素
Entry<K,V> current;
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;
}
}
//value的迭代器
private final class ValueIterator extends HashIterator<V> {
public V next() {
return nextEntry().value;
}
}
//key的迭代器
private final class KeyIterator extends HashIterator<K> {
public K next() {
return nextEntry().getKey();
}
}
//entry的迭代器
private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
public Map.Entry<K,V> next() {
return nextEntry();
}
}
//返回key的迭代器 可用于子类重写
Iterator<K> newKeyIterator() {
return new KeyIterator();
}
Iterator<V> newValueIterator() {
return new ValueIterator();
}
Iterator<Map.Entry<K,V>> newEntryIterator() {
return new EntryIterator();
}
// Views
private transient Set<Map.Entry<K,V>> entrySet = null;
//这个keySet是从abstractSet继承过来的 这里只是简单的返回了一个引用 只有在使用迭代器的情况下才能获得所有key
public Set<K> keySet() {
Set<K> ks = keySet;
return (ks != null ? ks : (keySet = new KeySet()));
}
private final class KeySet extends AbstractSet<K> {
public Iterator<K> iterator() {
//重点在于nextEntry这个方法
return newKeyIterator();
}
public int size() {
return size;
}
public boolean contains(Object o) {
return containsKey(o);
}
public boolean remove(Object o) {
return HashMap.this.removeEntryForKey(o) != null;
}
public void clear() {
HashMap.this.clear();
}
}
//返回value的集合
public Collection<V> values() {
Collection<V> vs = values;
return (vs != null ? vs : (values = new Values()));
}
private final class Values extends AbstractCollection<V> {
public Iterator<V> iterator() {
//重点在于nextEntry这个方法
return newValueIterator();
}
public int size() {
return size;
}
public boolean contains(Object o) {
return containsValue(o);
}
public void clear() {
HashMap.this.clear();
}
}
//返回所有的键值对
public Set<Map.Entry<K,V>> entrySet() {
return entrySet0();
}
private Set<Map.Entry<K,V>> entrySet0() {
Set<Map.Entry<K,V>> es = entrySet;
return es != null ? es : (entrySet = new EntrySet());
}
private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
public Iterator<Map.Entry<K,V>> iterator() {
return newEntryIterator();
}
public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<K,V> e = (Map.Entry<K,V>) o;
Entry<K,V> candidate = getEntry(e.getKey());
return candidate != null && candidate.equals(e);
}
public boolean remove(Object o) {
return removeMapping(o) != null;
}
public int size() {
return size;
}
public void clear() {
HashMap.this.clear();
}
}
// java.io.Serializable的读取函数:根据写入方式读出
// 将HashMap的“总的容量,实际容量,所有的Entry”依次读出
private void writeObject(java.io.ObjectOutputStream s)
throws IOException
{
Iterator<Map.Entry<K,V>> i =
(size > 0) ? entrySet0().iterator() : null;
// Write out the threshold, loadfactor, and any hidden stuff
s.defaultWriteObject();
// Write out number of buckets
s.writeInt(table.length);
// Write out size (number of Mappings)
s.writeInt(size);
// Write out keys and values (alternating)
if (i != null) {
while (i.hasNext()) {
Map.Entry<K,V> e = i.next();
s.writeObject(e.getKey());
s.writeObject(e.getValue());
}
}
}
private static final long serialVersionUID = 362498820763181265L;
// java.io.Serializable的读取函数:根据写入方式读出
// 将HashMap的“总的容量,实际容量,所有的Entry”依次读出
private void readObject(java.io.ObjectInputStream s)
throws IOException, ClassNotFoundException
{
// Read in the threshold, loadfactor, and any hidden stuff
s.defaultReadObject();
// Read in number of buckets and allocate the bucket array;
int numBuckets = s.readInt();
table = new Entry[numBuckets];
init(); // Give subclass a chance to do its thing.
// Read in size (number of Mappings)
int size = s.readInt();
// Read the keys and values, and put the mappings in the HashMap
for (int i=0; i<size; i++) {
K key = (K) s.readObject();
V value = (V) s.readObject();
putForCreate(key, value);
}
}
// These methods are used when serializing HashSets
int capacity() { return table.length; }
float loadFactor() { return loadFactor; }
}
- HashMap的容量为什么为2的整数次幂?
首先看一下HshMap的put方法,它会利用hash这个方法,根据键值对中key的哈希值计算出一个整数,然后在根据这个整数和(数组的长度-1)去计算出 这个键值对应该存储在数组的哪个索引上。
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;
}
重点就在于这个indexFor方法上:
h: key的哈希值经过hash()方法计算出来的结果
length:HashMap的容量
static int indexFor(int h, int length) {
return h & (length-1);
}
这个方法用于计算键值对存储在数组上的位置,
一方面,h & (length-1)的实质就是h对length的取模运算,这里采用了前者,是因为在计算机中位运算的效率高于取余运算。不过h & (length-1)和h%length相等的条件就是 length 必须为2的次幂。
另一方面, 因为HashMap是简单来说就是数组+链表的结构,为了保证其空间的利用率和查询速度,就希望在每个链表上存储的键值对的数量基本保持一致。index = h & (length-1); index代表了这个键值对在这个数组中的索引。也就是说我们希望在h为随机数的情况下,我们希望index的值在0到length这个区间内是随机的,不会漏掉哪几个,而使得数组的那个位置会永远为空。
而h和length-1之家做的与运算。因为h可能是任何值,我们就也要保证length-1的值不会影响计算结果的出现概率。
当length为2的整数次幂的时候,length-1的二进制为1,11,111,1111,...这样的形式。
这样,在决定index的值上面,h就有更大的发言权。
举个列子,如果length的值不是2的整数次幂,而是15,(length-1)的二进制为
1110.它的尾数为0,这样在进行&运算的时候,无论h的值为什么,0001、1001、1011等尾数为1的位置就永远不会被键值对所占据,会造成空间的浪费。
-
关于HashMap的负载因子
HashMap中默认的负载因子为0.75.这个负载因子决定HashMap是否需要进行扩容操作。当然,这个HashMap的容量满了也需要进行扩容。但默认的为当键值对的数量达到(HashMap的容量*0.75)的时候就进行扩容。 负载因子太高,空间的利用率高了,但是会增加查询的成本(put,get方法都会去遍历链表)。负载因子太低了,而空间的利用率又降低了。所以只能在这两者之间取一个折中。 JDK1.8中HashMap的机制有了很大变动,需要重新看一下。 