我正在使用 HashMap: byte[] 键和 String 值。但我意识到,即使我通过使用放置相同的对象(相同的字节数组和相同的字符串值)
myList.put(TheSameByteArray, TheSameStringValue)
进入HashMap,表中仍然插入了一个具有不同HashMapEntry的新对象。然后函数 containsKey() 无法工作。
有人可以为我解释一下吗?我怎样才能解决这个问题?谢谢。(安卓Java)
@Override public boolean containsKey(Object key) {
if (key == null) {
return entryForNullKey != null;
}
int hash = Collections.secondaryHash(key);
HashMapEntry<K, V>[] tab = table;
for (HashMapEntry<K, V> e = tab[hash & (tab.length - 1)];
e != null; e = e.next) {
K eKey = e.key;
if (eKey == key || (e.hash == hash && key.equals(eKey))) {
return true;
}
}
return false;
}