参见英文答案 >
What issues should be considered when overriding equals and hashCode in Java?11个
我创建了一个HashMap,其中Student为键,String为value.
我创建了一个HashMap,其中Student为键,String为value.
现在到处都读过如果用作hashmap的键,有必要重写equals和hashcode方法.
但我没有覆盖它.并在hashmap中插入多个键值对.
我也能把它取回来.
那为什么有必要呢?
解决方法
当您希望将对象用作使用散列的集合中的有用散列键时,它们是必需的.
您是否尝试添加有意义相等的Student对象?
首先要了解significance of equals method in java