Why should I override hashCode() when I override equals() method?

后端 未结 5 2101
夕颜
夕颜 2020-11-27 05:22

Ok, I have heard from many places and sources that whenever I override the equals() method, I need to override the hashCode() method as well. But consider the following piec

5条回答
  •  轮回少年
    2020-11-27 06:10

    Because HashMap/Hashtable will lookup object by hashCode() first.

    If they are not the same, hashmap will assert object are not the same and return not exists in the map.

提交回复
热议问题