简体   繁体   English

是否应该重写哈希码以将对象存储在ArrayList中?

[英]Should one override hashcode to store objects in ArrayList?

Should we override hashCode() method to store a user-defined object in an ArrayList. 我们是否应该重写hashCode()方法以将用户定义的对象存储在ArrayList中。 If there is a class called Employee in which two fields id and name are there. 如果有一个名为Employee的类,其中有两个字段id和name。

hashCode未被ArrayList ,但是equals用于确定对象是否位于List中,并且每当重写equals ,也应该重写hashCode ,因为hashCode对象应该具有相同的hashCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM