//hashMap是位于java.util包路径下面的
package java.util;
//该类实现了Map几口以及继承了AbstractMap类
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
//属性中默认的初始容量为16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
//属性中允许最大的容量值
static final int MAXIMUM_CAPACITY = 1 << 30;
//默认的负载因子为0.75
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//在数组下面默认最大的元素个数,大于直接调用红黑树
static final int TREEIFY_THRESHOLD = 8;
第二点:就是内部类Node的定义,不同于LinkedList,其实hashMap底层源码中就是Node数组
//可以看见这个node类实现了Map.Entry接口,在这个类中主要有四个属性
//hash, key , value, Node<K,V> next
// hash值,next- 下一个node对象
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
//这就是HashMap中的数据结构
transient Node<K,V>[] table;
具体的构造方法-有参以及无参构造
获取元素以及put元素,remove元素以及底层源码的实现
//put方法中只是调用了putVal方法来插入元素
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
//putVal方法主要的就是将元素添加进去
//首先判断数组是否为null,以及长度是否为0,如果满足任意条件就reszie方法
//判断是否在索引处是否为null值,如果是就添加进数组,如果不是就赋值而已
//再判断是否为TreeNode节点,如果是就添加进TreeNode
//再判断查询出来的下一个Node是否为Null如果为null就又插入一条新数据
//判断p上面挂载的节点是否超过8个,如果超过就又引入红黑树
//然后如果在循环中找到符合条件的数据直接覆盖掉
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//在这里的时候就是覆盖掉前面的元素了
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
//删除其中的key值
//主要的方法就是下面的removeNode方法
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
//下面主要分析removeNode方法的源码分析
//代码逻辑首先是根据hash,key求出位于数组的索引值,如果第一个Node对象刚好满足条件就赋值给node = p
//然后就是往节点循环,将node == p,第一次之外
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
else if ((e = p.next) != null) {
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
else {
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
//真正的删除操作是在这里进行元素排序的
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}
//根据key值获取value值查询
//在这其中的能够看见主要的一个方法是getNode方法
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
//这里主要的是对getNode方法进行源码分析
//在这个方法中,代码逻辑也是根据hash得到数组索引,然后获取到一个Node对象,然后就是判断是否符合条件如过符合就返回这个对象,再继续判断是否有next对象,这里我不明白的一点就是为什么还要判断是否是TreeNode类型
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
}
总结:新增key值时,如果根据hash与key计算出数组的索引值时重复那么就不新增,如果为null值就添加进去,如果是红黑树就直接调用putTreeVal方法进行添加,然后就继续遍历链表如果p.next为空时就直接添加进去,如果出现重复就会替换掉数组的表头元素,直接替换掉
注意到,HashMap中的方法都是非线程安全的