collections - Why does java.util.TreeMap.KeySet not implement equals? -
i check if keys of 2 treemaps equal. calling
mytreemap.navigablekeyset()
i receive java.util.treemap.keyset
implements navigableset
, in turn extends sortedset
. hence expect 2 keysets considered equals if not contain same elements (as checked equals of abstractset
) also in same order. can't see how restriction enforced class keyset
. miss?
you cannot make guarantee.
while navigableset
, extends sortedset
, guarantees elements in order defined elements (if implement comparable
) or given comparator
, not override set
's contract .equals()
. , set
makes no ordering guarantee.
you can have 2 sortedset
s same elements, comparison different, will equal.
the way can check element ordering slurp elements of both sets list
s , check these lists equal -- because list
does guarantee element order in .equals()
contract.
note if keyset
class did override .equals()
(and therefore .hashcode()
well) check element order, no longer able pretend implement set
!
Comments
Post a Comment