Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1372,12 +1372,20 @@ public boolean equals(Object o) {
Node<K,V>[] t;
int f = (t = table) == null ? 0 : t.length;
Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
for (Node<K,V> p; (p = it.advance()) != null; ) {
V val = p.val;
Object v = m.get(p.key);
if (v == null || (v != val && !v.equals(val)))
return false;

try {
for (Node<K,V> p; (p = it.advance()) != null; ) {
V val = p.val;
Object v = m.get(p.key);
if (v == null || (v != val && !v.equals(val)))
return false;
}
} catch (ClassCastException | NullPointerException _) {
// m.get(p.key) is contractually allowed to throw CCE or NPE
// but CHM doesn't allow null keys, so NPE shouldn't occur in practice
return false;
}

for (Map.Entry<?,?> e : m.entrySet()) {
Object mk, mv, v;
if ((mk = e.getKey()) == null ||
Expand Down
19 changes: 19 additions & 0 deletions test/jdk/java/util/concurrent/tck/ConcurrentHashMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
Expand Down Expand Up @@ -878,6 +879,24 @@ public void testReentrantComputeIfAbsent() {
} catch (IllegalStateException success) {}
}

public void testMapEqualsIfClassCastExceptionOccurs() {
class MonotypeKeyMap extends HashMap<Byte, Object> {
@Override public Object get(Object key) {
return super.get((Byte)key); // Force cast, allowed by spec
}
}

var mkm = new MonotypeKeyMap();
mkm.put((byte)1, "value1");
var similar = new ConcurrentHashMap<Byte, Object>();
similar.put((byte)1, "value1");
var different = new ConcurrentHashMap<String, String>();
different.put("test1", "value1");

assertTrue(similar.equals(mkm));
assertFalse(different.equals(mkm));
}

private static Item findValue(ConcurrentHashMap<Item, Item> map,
Item key) {
return (key.value % 5 == 0) ? key :
Expand Down