I try to use the containsKey
method of a TreeMap
, but somehow I have problems with it.
The objects stored in the treemap are defined such as equals()
does not deliver the same result as compareTo()
. this is intended.
However, the doc of java.util.Map
says:
Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that
(key==null ? k==null : key.equals(k))
. (There can be at most one such mapping.)
So I tried following:
c = someModifiedObject();
boolean t1 = sm.containsKey(c);
someObject c2 = new someObject();
boolean t2 = sm.containsKey(c2);
boolean t3 = c.equals(new Chain());
int t4 = c.compareTo(new Chain());
t1 is true, as the object is in the treemap.
t3 is true, as t1 is equal t3 (regarding to the change equals()
operator)
t4 is false
however, t2 is false as well. It seems that TreeMap
uses compareTo()
rather than equals()
to determine if the object is present.
Is there another implementation of a sorted map, where i can use equals()
to check if an object exists?