简体   繁体   English

对覆盖“ AbstractMap.SimpleEntry <>”中的“ equals()”有什么意义吗? “ equals()”应该是最终的吗?

[英]Any point to overriding “equals()” in “AbstractMap.SimpleEntry<>”? “equals()” should be final?

Now, I am using guava collections, and am all good and happy. 现在,我使用的是番石榴,非常高兴。 But, I want to understand why my own code did not work. 但是,我想了解为什么我自己的代码无法正常工作。 I think I was trying to do the impossible: 我想我正在尝试做不可能的事情:

I created a SortedSet<Map.Entry<K, V>> data structure. 我创建了SortedSet<Map.Entry<K, V>>数据结构。 The bug was that I sometimes got duplicates in the Set. 漏洞是我有时在Set中出现重复项。 For me, a "duplicate" Map.Entry was one where its key matched another entry's key. 对我而言,“重复的” Map.Entry是一个其键与另一个条目的键匹配的键。 I wanted to ignore the value with regard to calculating equality. 我想忽略有关计算平等的价值。

But, here is the API: 但是,这是API:
AbstractMap.SimpleEntry : public boolean equals(Object o) AbstractMap.SimpleEntry:公共布尔值等于(对象o)

I was like "No. I don't want that." 我就像“不。我不要那样。” I tried to bend SimpleEntry to my will by extending it and overriding equals() so as to only use the keys to decide equality. 我试图通过扩展它并覆盖equals()来使SimpleEntry随心所欲,以便仅使用键来确定相等性。

question
Making AbstractMap.SimpleEntry only use the key to decide equality had some sort of ripple effect that would be almost impossible to fully understand? 使AbstractMap.SimpleEntry仅使用键来确定相等是否具有某种连锁反应,这几乎是不可能完全理解的?

Is there absolutely any possible reason to override equals() in AbstractMap.SimpleEntry? 绝对有任何可能的理由在AbstractMap.SimpleEntry中重写equals()吗? The javadoc is so restrictive with deciding equality, shouldn't equals() just be made final ? javadoc在决定相等性方面是如此严格,难道不应该将equals() 最终定型吗?

It's conceivable that you might implement equals in some more optimized way, such as if you'd precached the hash code for the key. 可以想象 ,您可以通过一些更优化的方式来实现equals ,例如是否预先缓存了密钥的哈希码。 Otherwise, yes, it should be considered final. 否则,是的,应该认为是最终的。 It is a part of the Map.Entry contract that equality must be tested on both key and the value. 这是Map.Entry合同的一部分,必须在键和值上测试相等性。

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

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