hashCode一直不怎么理解,今天就详细的看了一下。
先贴代码:
HashMap map = new HashMap();
map.put("ab", 1);
map.put("2", 2);
System.out.println("map.hashcode:"+map.hashCode());
输出结果是map.hashcode:3152
哦,现在知道了,我们定义的map的哈希值是3152.
这个结果是怎么算出来的呢?
首先看一下执行的hashCode方法:
public int hashCode() {
int h = 0;
Iterator<Entry<K,V>> i = entrySet().iterator();
while (i.hasNext())
h += i.next().hashCode();
return h;
}
也就是说,只要获取了entrySet中的每一个hashCode,相加就可以得到map的hashCode。
这个entrySet的代码继续贴一下:
public Set<Map.Entry<K,V>> entrySet() {
return entrySet0();
}
private Set<Map.Entry<K,V>> entrySet0() {
Set<Map.Entry<K,V>> es = entrySet;
return es != null ? es : (entrySet = new EntrySet());
}
也就是获取一下这个map的Entry集合。
到这步总结下:
获取一个map的哈希值,就是获取map中的每个Entry,将每个Entry的哈希值求出来再相加即可。
继续来看Entry的定义:
在Map类中:
interface Entry<K,V> {
K getKey();
V getValue();
V setValue(V value);
boolean equals(Object o);
int hashCode();
}
在HashMap类中:
static class Entry<K,V> implements Map.Entry<K,V> {
/**
.
.
.
其他的代码省略
*/
public final int hashCode() {
return (key==null ? 0 : key.hashCode()) ^
(value==null ? 0 : value.hashCode());
}
}
来了,每个Entry的hashCode怎么算?就是key的hashCode异或value的hashCode。
好的,现在回到刚刚的例子。
map的哈希值是啥?
"ab".hashCode()^1.hashCode()
+
"2".hashCode^2.hashCode()
关于String和Integer的hashcode方法我就不贴了,负责任的告诉你,上面的值:
0b 110000100001 ^ 0b 1
+
0b 110010 ^ 0b 10
=
0b 110000100000
+
0b 110000
=
3104
+
48
=
3152
终于算出来了。
关于哈希的优点,过几天再说。