Why AbstractCollection does not implement equals()?

前端 未结 3 472
时光说笑
时光说笑 2021-01-18 05:49

Did you know that :

Map m1 = new HashMap();
Map m2 = new HashMap();
Sys         


        
3条回答
  •  猫巷女王i
    2021-01-18 06:31

    Both AbstractList and AbstractSet extend AbstractCollection, and they have different behaviors for their equals() methods, specified by the interfaces List and Set. The interface for Collection says:

    While the Collection interface adds no stipulations to the general contract for the Object.equals, programmers who implement the Collection interface "directly" (in other words, create a class that is a Collection but is not a Set or a List) must exercise care if they choose to override the Object.equals.

    So AbstractCollection should definitely not override equals(). That said, I don't really know why HashMap$Values would not implement equals() itself.

提交回复
热议问题