Java

Java

Made by DeepSource

Hashtable/ConcurrentHashMap.contains() checks for whether a value exists, not keys JAVA-E1098

Bug risk
Critical

A call to Hashtable.contains() or to ConcurrentHashMap.contains() was detected where the object passed to the method was of the same type as the key of the concerned map. It is likely this should be replaced with a containsKey call instead.

Hashtable.contains() and ConcurrentHashMap.contains() are both legacy methods that check for whether the argument is present in the value set of the map structure, and not the key set.

Bad Practice

Avoid using contains() to check for the presence of a key:

ConcurrentHashMap<Integer, UUID> chm = ...;

// This would never be true, because a UUID is not an Integer!
if (chm.contains(32)) {
    // ...
}

Recommended

To fix this problem, use the containsKey() method instead.

if (chm.containsKey(32)) {
    // ...
}

References