简体   繁体   English

HashSet1.retainAll(HashSet2);如何 在幕后工作?

[英]How does HashSet1.retainAll(HashSet2); work behind the scenes?

How does HashSet1.retainAll(HashSet2); HashSet1.retainAll(HashSet2);如何 work behind the scenes? 在幕后工作?

I am adding objects with identical parameters to two different HashSet , but when I use the above I don't seem to get the right result. 我正在将具有相同参数的对象添加到两个不同的HashSet ,但是当我使用上述对象时,似乎并没有得到正确的结果。 Ie it doesn't detect it's actually the same parameters in the object. 也就是说,它不会检测到它实际上与对象中的参数相同。

Is this method above comparing the actual address of my objects in the set? 上面的方法是否比较集合中对象的实际地址?

If so, how can I go about making it compare parameters of the objects instead. 如果是这样,我该如何去代替它比较对象的参数。

Perhaps override equals? 也许超越等于?

I have overwritten the hashCode and .equals methods in my class and this has resulted in the method comparing the correct parameter's rather than object address's. 我已经在类中覆盖了hashCode和.equals方法,这导致该方法比较了正确的参数而不是对象地址。 Thanks goes to @marstran for his advice on this matter. 感谢@marstran在此问题上的建议。

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

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