HashMap底层是通过数组加链表的结构来实现的。
HashMap 的实例有两个参数影响其性能:“初始容量” 和 “加载因子”。
初始容量只是哈希表在创建时的容量。加载因子 是哈希表在其容量自动增加之前可以达到多满的一种尺度。当哈希表中的条目数超出了加载因子与当前容量的乘积时,通过调用 rehash 方法将容量翻倍。
说的通俗一点啊 比如说你要装水 你首先找个一个桶 这个桶的容量就是加载容量,加载因子就是比如说你要控制在这个桶中的水要不超过水桶容量的多少,比如加载因子是0.75 那么在装水的时候这个桶最多能装到3/4 处, 这么已定义的话 你的桶就最多能装水 = 桶的容量 * 加载因子
如果桶的容量是40 加载因子是0.75 那么你的桶最多能装40*0.75 = 30的水
如果你要装的水比30 多 那么就该用大一点的桶 而rehash就是负责增加桶的容量的方法
HashMap包括几个重要的成员变量:table, size, threshold, loadFactor, modCount。
table是一个Entry[]数组类型,而Entry实际上就是一个单向链表。哈希表的"key-value键值对"都是存储在Entry数组中的。
size是HashMap的大小,它是HashMap保存的键值对的数量。
threshold是HashMap的阈值,用于判断是否需要调整HashMap的容量。threshold的值="容量*加载因子",当HashMap中存 储数据的数量达到threshold时,就需要将HashMap的容量加倍。
loadFactor就是加载因子。
modCount是用来实现fail-fast机制的。
HashMap通过计算key的hash值。当hash值相同时,就会出现hash冲突,HashMap通过链表来解决冲突。
示例:
public class HashMapTest { public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("key", "value"); System.out.println(map.get("key")); } }
1、put方法分析
//添加键值对方法
public V put(K key, V value) {
//如果key为null,则hash值为0,将这个entry放在索引为0的地方,即table[0]
if (key == null)
return putForNullKey(value);
//key不为null
int hash = hash(key.hashCode());//计算key的hashCode的hash值
int i = indexFor(hash, table.length);//返回hash值所在的索引位置
//遍历table[i]处的Entry,若存在key相同并且hash值相同,则用新元素替换旧元素
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;
}
}
//修改标记+1,然后进行元素添加操作
modCount++;
addEntry(hash, key, value, i);//将包含特定key、value和hash值的entry添加到特定的桶中
return null;
}
//添加key为null的元素
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;
}
/**
* Adds a new entry with the specified key, value and hash code to
* the specified bucket. It is the responsibility of this
* method to resize the table if appropriate.
*
* Subclass overrides this to alter the behavior of put method.
*/
void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}
2、get方法分析
/**
* 返回映射的key对应的value值,若map不包含该key,返回null
*/
public V get(Object key) {
if (key == null)//获取key为null的value值
return getForNullKey();
int hash = hash(key.hashCode());//计算key的hashCode的hash值
//遍历数组中对应hash值的Entry链表,若Entry元素的hash值与hashCode的hash值相等并且key与查找的key相等,则返回此Entry元素的value值
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;
}
qq:1992093891