jdk源码——HashMap
Posted wanglingdeboke
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdk源码——HashMap相关的知识,希望对你有一定的参考价值。
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冲突问题
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;
}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))) {
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);
hash = (null != key) ? hash(key) : 0;
bucketIndex = indexFor(hash, table.length);
}createEntry(hash, key, value, bucketIndex);
}
void createEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);//新增一个节点
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);
}
以上是关于jdk源码——HashMap的主要内容,如果未能解决你的问题,请参考以下文章