简体   繁体   English

如何覆盖等于和哈希码到列表 <Long> 参数

[英]How to override equals and hashcode to List<Long> parameter

I have one class attribute called groups: 我有一个称为组的类属性:

private List<Long> groups;

Can i use this attribute inside my equals like this: 我可以在我的equals中使用这个属性吗?

if(!groups.equals(other.groups))
  return false

The answer to your updated question is: it depends on particular criterias for equality. 您最新问题的答案是:它取决于平等的特定标准。

Default equals method for list compares size and all corresponding elements. 默认的equals方法用于列表比较大小和所有对应的元素。 It means that if lists have the same elements but in different orders they are not equal. 这意味着,如果列表具有相同的元素,但顺序不同,则它们是不相等的。 If it fits your equality conditions then you can surely use it. 如果它符合您的平等条件,那么您肯定可以使用它。 If not, you have to compare lists manually according to your needs. 如果不是,则必须根据需要手动比较列表。

You cannot. 你不能。 The List interface specifies its equals and hashCode contract and any implementation that changed that would be in violation. List接口指定其equalshashCode契约以及任何可能违反的更改实现。

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

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