一、hashcode 和 equals關(guān)系
先以一個(gè)小例子入手:
public class HashTest {
public static void main(String[] args) {
HashMap<Book,String> map = new HashMap<>();
Book b = new Book(1,"java");
map.put(b, "String");
System.out.println((b.hashCode() +" "+ new Book(1,"java").hashCode()));
System.out.println(map.get(new Book(1,"java")));
}
}
class Book {
public int price;
public String name;
public Book(int price, String name) {
super();
this.price = price;
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
這是個(gè)只重寫(xiě)了equals(),并沒(méi)有重寫(xiě)hashcode()。上面運(yùn)行的結(jié)果如下:
2018699554 1311053135
null
也就是沒(méi)有重寫(xiě)的hashcode值,是系統(tǒng)處理的,內(nèi)容一致的對(duì)象hashcode并不一定相同。第二點(diǎn)就是HashMap會(huì)根據(jù)Key的hashcode來(lái)決定放在哪個(gè)數(shù)組中,不同的hashcode當(dāng)然得不到想要的Value。
對(duì)于hashcod()和equals()幾點(diǎn)建議:
- equals相等,它的hashcode也一定要相等
- 重寫(xiě)了hashcode,也一定要重寫(xiě)equals
- equals要滿足對(duì)稱(chēng)、反射、傳遞特性
二、HashMap

HashMap的存儲(chǔ)方式是數(shù)組+鏈表的形式,數(shù)組也叫作桶,hashcode | (數(shù)組大小-1)相等的Key放在同一個(gè)鏈表中。先看幾個(gè)屬性定義:
//默認(rèn)數(shù)組的長(zhǎng)度
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
// 數(shù)組的最大上限
static final int MAXIMUM_CAPACITY = 1 << 30;
// 默認(rèn)加載因子,如果DEFAULT_LOAD_FACTOR * CAPACITY < 元素個(gè)數(shù),就需要擴(kuò)容調(diào)整
static final float DEFAULT_LOAD_FACTOR = 0.75f;
// 鏈表的結(jié)構(gòu)定義
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
}
首先最常見(jiàn)的問(wèn)題是:數(shù)組的長(zhǎng)度為什么只能是2的次方大?。?/strong>
// 使用低16位避免沖突
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
如何根據(jù)Key的hashcode,將元素盡量平均的放入了桶中。如果采用非2次方會(huì)有什么影響了?
假設(shè)是數(shù)組的長(zhǎng)度是15,那么15 - 1 = 1110;在與1110作與運(yùn)算的時(shí)候,尾部永遠(yuǎn)都是0,事實(shí)上只有三位數(shù)有效,那么長(zhǎng)度為15的數(shù)組只有8個(gè)用到了,剩下的都浪費(fèi)了。那么采用除以長(zhǎng)度取余的方式不就可以解決數(shù)組浪費(fèi)的問(wèn)題了嘛,但是取余的效率比按位與的方式差多了。
小結(jié):
- 不是2的次方,會(huì)造成數(shù)組資源浪費(fèi)的問(wèn)題
- 采用取余的方法,沒(méi)有按位與的效率高
HashMap的添加操作put
添加操作是HashMap的核心,會(huì)涉及到鏈表插入和重新擴(kuò)容。
HashMap是支持null作為key或者value的
// 添加入口
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
// 查找Key是不是之前插入過(guò)了,插入過(guò)了就直接返回,否則放在鏈表末尾
// 如果數(shù)組還沒(méi)初始化,則要resize()操作,如果單鏈表長(zhǎng)度過(guò)大則treeifyBin(tab, hash),將其樹(shù)化存儲(chǔ);
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
// 如果桶為null或長(zhǎng)度為0,需要重新分配大小,相當(dāng)于懶加載模式,使用時(shí)才初始化
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
// 核心 按位與來(lái)查找元素在哪個(gè)桶上面,該桶的元素還沒(méi)有,直接放在第一個(gè)即可
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
// 找到該鏈表的第一個(gè)元素跟插入的元素的key是一樣的,說(shuō)明原本就插入過(guò)這個(gè)key
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);
// 尋找之前有沒(méi)有插入過(guò)這個(gè)key ,如果e不為null就代表有
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
// 鏈表長(zhǎng)度過(guò)大,也需要調(diào)節(jié)數(shù)組
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
// existing mapping for key
if (e != null) {
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
現(xiàn)在看下擴(kuò)容的方法resize(),遍歷數(shù)組的,在遞歸鏈表,取出子元素按照新的規(guī)范存儲(chǔ)
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
// 數(shù)組擴(kuò)大一倍
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
// 設(shè)置并檢查新的閾值
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
if (oldTab != null) {
// 通過(guò)循環(huán)遍歷的方式將數(shù)據(jù)重新分配到新的數(shù)組桶中
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode) // 針對(duì)樹(shù)化的部分進(jìn)行分配
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}