JDK1.7
从源码上看,HashMap 实现了Map接口 cloneable接口,和序列化接口
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
{
HashMap的默认初始容量为16
static final int DEFAULT_INITIAL_CAPACITY = 16;
HashMap最大容量为2^30
static final int MAXIMUM_CAPACITY = 1 << 30;
加载因子为0.75,当容量达到75%时就扩容一次
static final float DEFAULT_LOAD_FACTOR = 0.75f;
The table, resized as necessary. Length MUST Always be a power of two.
长度必须为2的幂
transient Entry<K,V>[] table;
The next size value at which to resize (capacity * load factor).
HashMap调整大小的下一个值
int threshold;
The load factor for the hash table.
final float loadFactor;
HashMap初始化
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)//保证容量为2的n次幂
capacity <<= 1;this.loadFactor = loadFactor;
threshold = (int)Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);//保存HashMap下一次调整大小的值
table = new Entry[capacity];//Entry是一个静态内部类
useAltHashing = sun.misc.VM.isBooted() &&
(capacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
init();
}
//Entry如下,get set方法
Entry类的定义为什么这么像一个链表,那么需要我们了HashMap的数据结构是 数组+链表的结构,数组的每个元素后面缀一个链表,为了解决hash冲突问题
hashMap的数据结构如上图所示,知道了这个,那么对于hashMap的put操作,就很容易明白了
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) {//entry是链表结构
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;
}public final boolean equals(Object o) {//判断是否相等,判断依据是key相同,value相同
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() {//若是key,value不为空返回当前key的hashcode与value的hashcode的异或的值
return (key==null ? 0 : key.hashCode()) ^
(value==null ? 0 : value.hashCode());
}public final String toString() {//toString方法
return getKey() + "=" + getValue();
}/**
* This method is invoked whenever the value in an entry is
* overwritten by an invocation of put(k,v) for a key k that's already
* in the HashMap.
*/
void recordAccess(HashMap<K,V> m) {
}/**
* This method is invoked whenever the entry is
* removed from the table.
*/
void recordRemoval(HashMap<K,V> m) {
}
}
指定初始化容量
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);//调用 HashMap(int initialCapacity, float loadFactor)方法
//DEFAULT_LOAD_FACTOR为默认加载因子0.75
}
无参构造函数
public HashMap() {//默认初始容量 及默认加载因子
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}
根据key得到值
public V get(Object key) {
if (key == null)
return getForNullKey();
Entry<K,V> entry = getEntry(key);return null == entry ? null : entry.getValue();
}
final Entry<K,V> getEntry(Object key) {//遍历找到当前的Entry,将他返回
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;
}
static int indexFor(int h, int length) {
return h & (length-1);//计算entry的分布问题,与index = hash % length相同,当m满足 m=2^n,那么k % m = k & (m-1)
}
得到为空的key
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;//调用getEntry方法
}
put方法
public V put(K key, V value) {
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) {//遍历链表找到key对应的位置 ,替换操作
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {// 去除重复的key,若是重复则用新的value替换原来的 //value
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) {
if ((size >= threshold) && (null != table[bucketIndex])) {//触发hashMap扩容操作
resize(2 * table.length);//容量为目前的2倍
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);//找到数组中当前key的位置
}createEntry(hash, key, value, bucketIndex);
}
扩容操作
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];//新的数组,size为之前的2倍
boolean oldAltHashing = useAltHashing;
useAltHashing |= sun.misc.VM.isBooted() &&
(newCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
boolean rehash = oldAltHashing ^ useAltHashing;
transfer(newTable, rehash);
table = newTable;
threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}
void transfer(Entry[] newTable, boolean rehash) {
int newCapacity = newTable.length;
for (Entry<K,V> e : table) {//遍历数组
while(null != e) {//遍历链表
Entry<K,V> next = e.next;
if (rehash) {//满足rehash
e.hash = null == e.key ? 0 : hash(e.key);//重新计算hash值
}
int i = indexFor(e.hash, newCapacity);//重新计算节点的位置
e.next = newTable[i];//将e节点放大重新计算的数组位置上
newTable[i] = e;
e = next;
}
}
}
void createEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);//新增一个节点,新增节点放在table[bucketIndex],并把之前放 //在table[bucketIndex]的节点置为新节点的next
size++;
}
为key为null的赋值value
private V putForNullKey(V value) {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {//key==null,放在数组的第0位
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);//与put同理
return null;
}
putForCreate
private void putForCreate(K key, V value) {
int hash = null == key ? 0 : 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 != null && key.equals(k)))) {
e.value = value;
return;
}
}createEntry(hash, key, value, i);
}
hash值得算法
1.8的源码
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
将hashCode右移16位 再原hashCode进行异或运算,算出key的hash值,这是为了当length比较小的时候,也能保证考虑到高低Bit位都参与到Hash的计算中,同时不会有太大的开销。
然后计算bucket的位置,h&(table.length-1)这样的操作是和 h%length 相同,当m满足 m=2^n,那么k % m = k & (m-1)