hashcode & equals
hashcode 和equals在hash结构中十分重要,前者用于快速定位,后者用于比较替换
hashcode标准写法
override fun hashCode(): Int {
//用31的好处:VM会自动把它转化成位运算(cpu支持的运算),运行速度快
var result = sourceKey.hashCode()
result += 31 * result + signature.hashCode()
result += 31 * result + width
result += 31 * result + height
appliedTransformation?.let {
result += 31 * result + it.hashCode()
}
result += 31 * result + decodedResourceClass.hashCode()
result += 31 * result + options.hashCode()
return result
}
复制代码
equals 标准写法
override fun equals(o: Any?): Boolean {
if (o is ResourceCacheKey) {
return o.let {
//kotlin== 相当于java equals
return sourceKey == it.sourceKey &&
signature == it.signature &&
width == it.width &&
height == it.height &&
appliedTransformation == it.appliedTransformation &&
decodedResourceClass == it.decodedResourceClass &&
options == it.options
}
}
return false
}
复制代码
put方法分析
大致流程
- 通过hasdcode 计算出在hash表中存放的位置
- 如果该位置上没有节点,直接新建节点存放value
- 该位置上有节点,通过equals方法处理冲突
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
//onlyIfAbsent =false
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//表为空则通过resize方法建表
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//通过(n - 1) & hash 获取在表中index
if ((p = tab[i = (n - 1) & hash]) == null)
//当前位置没有值,则新建node存放value
tab[i] = newNode(hash, key, value, null);
else {
//当前位置有值则通过equals处理冲突
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
//hash是通过hashcode计算而来,所以hashcode相等并且equals相等
//则将e指向p,用于待会将value替换
e = p;
else if (p instanceof TreeNode)
//hashcode相等,equals不相等情况
//存到红黑树中
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
//hashcode相等,equals不相等情况
//遍历单链表
for (int binCount = 0; ; ++binCount) {
//遍历到单链表尾部
if ((e = p.next) == null) {
//在单链表尾部新建节点存放Node
p.next = newNode(hash, key, value, null);
//TREEIFY_THRESHOLD 默认为 8;
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
//超过8个,将节点树化
treeifyBin(tab, hash);
break;
}
//equals相等,则直接break ,待会替换新的value
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
//替换新的value
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
//onlyIfAbsent =false ,将value替换成新的
e.value = value;
afterNodeAccess(e);
//把oldValue返回
return oldValue;
}
}
++modCount;
if (++size > threshold)
//size 大于阀值则扩容
resize();
afterNodeInsertion(evict);
return null;
}
复制代码
get 方法分析
大致流程
- 通过hashcode计算在表中的位置index
- 遍历单链表或者红黑树,如果equals相等则返回value
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
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) {
//通过(n - 1) & hash 算出在表中的index , 如果不为null,则判断equals
if (first.hash == hash &&
((k = first.key) == key || (key != null && key.equals(k))))
//first 节点的equals 相等则返回first
return first;
//处理冲突的情况
if ((e = first.next) != null) {
//遍历红黑树找出equals相等的元素并返回
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
//遍历单链表找出equals相等的元素并返回
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
复制代码
总结
- put : 通过hashcode进行定位,然后通过equals处理冲突,如果eqauls相等则用新值替换旧值,不相等则用链表或者红黑树存放冲突值
- get : 通过hashcode进行定位,然后找到equals相等的值
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END