修改HashMap中作为键的可变对象会导致数据丢失?本文带你了解HashMap的原理及正确使用姿势。
原文标题:悲催,放到 Map 中的元素取不出来了
原文作者:阿里云开发者
冷月清谈:
程序员小明将一个Player对象作为键存入HashMap,之后修改了Player对象的name属性,导致根据修改后的name以及原Player对象都无法从HashMap中取出数据。
这是因为HashMap的put方法使用键的hashCode值来确定存储位置,修改键对象的属性值后,hashCode值也发生了变化,导致与初始存储位置不匹配。虽然对象仍然存在于HashMap中,但由于hashCode值的变化,使用containsKey或get方法都无法找到它。
文章深入分析了HashMap的put、containsKey和get方法的源码,解释了这些方法是如何使用哈希值进行操作的。
最后,文章给出了几点建议:永远不要修改HashMap中的键;采用防御性编程,将键类定义为不可变类型;自定义键类时谨慎重写equals和hashCode方法。
怜星夜思:
2、除了将Player类设置为不可变,还有哪些方法可以避免这个问题?
3、在实际项目中,如果遇到类似的情况,该如何快速排查和解决?
原文内容
阿里妹导读
本文通过一个程序员小明遇到的实际问题,深入探讨了在使用 HashMap 时由于键对象的可变性导致的数据访问异常。
如果你只想看结论,给你上个一句话省流版:
一、前言
师兄说:“莫慌,你且慢慢说来”
程序员小明说道:“我放到 Map 中的数据还在,但是怎么也取不出来了...”
师兄,于是帮小明看了他的代码,发现了很多不为人知的秘密....
二、场景复现
public class Player { private String name;
public Player(String name) {
this.name = name;
}// 省略了getter和setter方法
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Player)) {
return false;
}
Player player = (Player) o;
return name.equals(player.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
}
此时,有懂行的小伙伴已经看出了一点端倪
Map<Player, Integer> myMap = new HashMap<>(); Player kai = new Player("Kai"); Player tom = new Player("Tom"); Player amanda = new Player("Amanda"); myMap.put(kai, 42); myMap.put(amanda, 88); myMap.put(tom, 200); assertTrue(myMap.containsKey(kai));
// 将Kai的名字更改为Eric kai.setName("Eric"); assertEquals("Eric", kai.getName());
Player eric = new Player(“Eric”);
assertEquals(eric, kai);// 现在,map中既不包含Kai也不包含Eric:
assertFalse(myMap.containsKey(kai));
assertFalse(myMap.containsKey(eric));
assertNull(myMap.get(kai));
assertNull(myMap.get(eric));
// 然而 Player("Eric") 以依然存在: long ericCount = myMap.keySet() .stream() .filter(player -> player.getName() .equals("Eric")) .count(); assertEquals(1, ericCount);
三、源码浅析
3.1 put 方法
3.1.1 put 方法概述
/** * Associates the specified value with the specified key in this map. * If the map previously contained a mapping for the key, the old * value is replaced. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return the previous value associated with <tt>key</tt>, or * <tt>null</tt> if there was no mapping for <tt>key</tt>. * (A <tt>null</tt> return can also indicate that the map * previously associated <tt>null</tt> with <tt>key</tt>.) */ public V put(K key, V value) { return putVal(hash(key), key, value, false, true); }
3.1.2 hash 方法
/**
* Computes key.hashCode() and spreads (XORs) higher bits of hash
* to lower. Because the table uses power-of-two masking, sets of
* hashes that vary only in bits above the current mask will
* always collide. (Among known examples are sets of Float keys
* holding consecutive whole numbers in small tables.) So we
* apply a transform that spreads the impact of higher bits
* downward. There is a tradeoff between speed, utility, and
* quality of bit-spreading. Because many common sets of hashes
* are already reasonably distributed (so don't benefit from
* spreading), and because we use trees to handle large sets of
* collisions in bins, we just XOR some shifted bits in the
* cheapest possible way to reduce systematic lossage, as well as
* to incorporate impact of the highest bits that would otherwise
* never be used in index calculations because of table bounds.
*/
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
3.1.3 putVal 方法
/** * Implements Map.put and related methods. * * @param hash hash for key * @param key the key * @param value the value to put * @param onlyIfAbsent if true, don't change existing value * @param evict if false, the table is in creation mode. * @return previous value, or null if none */ final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { //定义了一个用于表示哈希表的数组 tab,一个节点 p 用于指向特定的哈希桶, // 以及两个整型变量 n 和 i 用于存储哈希表的大小和计算的索引位置。 Node<K,V>[] tab; Node<K,V> p; int n, i;
//如果哈希表未初始化或其长度为0,它将调用 resize() 方法来初始化或扩容哈希表。
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;
//如果首个节点是一个红黑树节点,则调用 putTreeVal 方法来处理。
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;
}
}
//如果找到了一个存在的节点,则根据 onlyIfAbsent 参数来决定是否要更新值,然后返回旧值。
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}//增加 modCount 来表示 HashMap 被修改了,并检查当前大小是否超过了阈值来决定是否要调整大小
++modCount;
if (++size > threshold)
resize();
//最后调用 afterNodeInsertion 方法(它在 HashMap 中是一个空方法,但在其子类 LinkedHashMap 中是有定义的),
//然后返回 null 来表示没有旧值。
afterNodeInsertion(evict);
return null;
}
3.2 containsKey 方法
3.2.1 containsKey 概览
java.util.HashMap#containsKey
/** * Returns <tt>true</tt> if this map contains a mapping for the * specified key. * * @param key The key whose presence in this map is to be tested * @return <tt>true</tt> if this map contains a mapping for the specified * key. */ public boolean containsKey(Object key) { return getNode(hash(key), key) != null; }
3.2.2 hash 方法
3.2.3 getNode 方法
/** * Implements Map.get and related methods. * * @param hash hash for key * @param key the key * @return the node, or null if none */ final Node<K,V> getNode(int hash, Object key) { //首先定义了一些变量,包括哈希表数组 tab、要查找的首个节点 first、 //一个辅助节点 e、数组的长度 n 和一个泛型类型的 k 用于暂存 key。 Node<K,V>[] tab; Node<K,V> first, e; int n; K k; //这里首先检查哈希表是否为空或长度是否大于 0 ,然后根据 hash 值找到对应的桶。 //(n - 1) & hash 这段代码是为了将 hash 值限制在数组的边界内,确保它能找到一个有效的桶。 if ((tab = table) != null && (n = tab.length) > 0 &&(first = tab[(n - 1) & hash]) != null) {
//检查第一个节点是否就是我们要找的节点,这里比较了 hash 值和 key。
//注意这里首先通过 == 来比较引用,如果失败了再通过 equals 方法来比较值,这样可以提高效率。
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
// 如果第一个节点不是我们要找的,就检查下一个节点是否存在。
if ((e = first.next) != null) {
//如果首个节点是一个树节点(即这个桶已经转换为红黑树结构),则调用 getTreeNode 方法来获取节点。
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
//这是一个 do-while 循环,用来遍历链表结构的桶中的每一个节点,直到找到匹配的节点或到达链表的尾部。
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
//如果没有找到匹配的节点,则返回 null。
return null;
}
3.3 get 方法
/** * Returns the value to which the specified key is mapped, * or {@code null} if this map contains no mapping for the key. * * <p>More formally, if this map contains a mapping from a key * {@code k} to a value {@code v} such that {@code (key==null ? k==null : * key.equals(k))}, then this method returns {@code v}; otherwise * it returns {@code null}. (There can be at most one such mapping.) * * <p>A return value of {@code null} does not <i>necessarily</i> * indicate that the map contains no mapping for the key; it's also * possible that the map explicitly maps the key to {@code null}. * The {@link #containsKey containsKey} operation may be used to * distinguish these two cases. * * @see #put(Object, Object) */ public V get(Object key) { Node<K,V> e; return (e = getNode(hash(key), key)) == null ? null : e.value; }
逻辑和 containsKey 一致,只是 getNode 之后,如果为 null 返回 null, 否则返回 e.value。
四、回归问题
注:下面的作图可能并不严谨,只是帮助理解,如有偏差请勿较真。
4.1 三次 put 后的效果
Map<Player, Integer> myMap = new HashMap<>(); Player kai = new Player("Kai"); Player tom = new Player("Tom"); Player amanda = new Player("Amanda");
myMap.put(kai, 42);
myMap.put(tom, 200);
myMap.put(amanda, 88);
assertTrue(myMap.containsKey(kai));
4.2 修改后
// 将Kai的名字更改为Eric kai.setName("Eric"); assertEquals("Eric", kai.getName());
4.3 执行判断
Player eric = new Player("Eric"); assertEquals(eric, kai);
// 现在,map中既不包含Kai也不包含Eric:
assertFalse(myMap.containsKey(kai));
assertFalse(myMap.containsKey(eric));
五、启示
5.1 永不修改 HashMap 中的键
-
哈希码更改
当你修改一个 HashMap 中的键时,该键的哈希码可能会更改,导致该键的哈希值不再与它当前所在的桶匹配。这将导致在使用该键进行查找时找不到相关的条目。 -
导致数据不一致
由于键的哈希码已更改,这将导致数据结构的不一致。这意味着,即使你能够以某种方式访问修改后的键,你也将得到一个不一致的映射,其中键不再映射到正确的值。 -
违反映射的契约
修改 HashMap 中的键实际上违反了 Map 接口的基本契约,即每个键都应该映射到一个值。通过更改键,你实际上是在不通过 put 或 remove 方法的情况下更改映射,这是不允许的。 -
可能导致内存泄漏
修改 HashMap 中的键可能还会导致内存泄漏问题。因为如果你失去了访问修改后的键的方式,那么该键及其对应的值将无法从 Map 中删除,从而导致内存泄漏。 -
破坏哈希表的性能
HashMap 依赖于均匀的哈希分布来实现其期望的时间复杂度。修改键可以破坏哈希分布,从而大大降低哈希表的性能。
5.2 防御性编程
public final class Player { // 不允许修改 private final String name;
public Player(String name) {
this.name = name;
}public String getName() {
return name;
}// 注意,我们没有提供setter方法
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Player)) {
return false;
}
Player player = (Player) o;
return name.equals(player.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
}
5.3 自定义键类时谨慎重写 equals 和 hashCode 方法
public class Player { private String name;
public Player(String name) {
this.name = name;
}// 省略了getter和setter方法
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Player)) {
return false;
}
Player player = (Player) o;
return name.equals(player.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
}
Map<Player, Integer> myMap = new HashMap<>(); Player kai1 = new Player("Kai"); Player kai2 = new Player("Kai"); myMap.put(kai1, 42); // 此时 kai2 覆盖了 kai1 的值 myMap.put(kai2, 88);
assertEquals(88,(int)myMap.get(kai1));
assertEquals(88,(int)myMap.get(kai2));
public class Player { private String name;
public Player(String name) {
this.name = name;
}// 省略了getter和setter方法
}
验证:
Map<Player, Integer> myMap = new HashMap<>(); Player kai1 = new Player("Kai"); Player kai2 = new Player("Kai"); myMap.put(kai1, 42); myMap.put(kai2, 88);
assertEquals(42,(int)myMap.get(kai1));
assertEquals(88,(int)myMap.get(kai2));
六、总结
每一个问题背后都是一个绝佳的学习机会。每一个奇奇怪怪的问题背后都有很多知识盲点。
希望大家可以抓住每一个问题知其然,知其所然,不断精进技术。
基于Hologres轻量高性能OLAP分析解决方案
本方案基于阿里云Hologres和DataWorks数据集成,通过简单的产品操作即可完成数据库RDS实时同步数据到Hologres,并通过Hologres强大的查询分析性能,完成一站式高性能的OLAP数据分析。
点击阅读原文查看详情。