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 sortedsets same elements, comparison different, will equal.

the way can check element ordering slurp elements of both sets lists , 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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -