Treemap uses compareTo instead of equals for containsKey ()

I try to use the method containsKeyfor TreeMap, but for some reason I have problems with it.

Objects stored in the treemap, defined as equals(), do not give the same result as compareTo(). it is provided.

However, the document java.util.Mapsays:

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 key k such that (key==null ? k==null : key.equals(k)). (This can be no more than one such mapping.)

So, I tried the 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 , treemap. t3 , t1 t3 ( equals()) t4 t2 . , TreeMap compareTo(), equals(), , .

, equals() ?

+3
1

java-API, , , Comparator, TreeMap Comparator. compareTo(), , ( , , equals()).

0

All Articles