English 中文(简体)
如果一刀切,不使用任何洗 has收集,就必须废除散装。
原标题:is it necessary to override hashcode if i am overriding equals and not using any hashing collection

I have an Object and i just want to override equals method to compare two objects, I am not using any hashing collection anywhere in my program. In such case why there is a contract in hashCode and equals method i.e. If two objects are equal then they should have the same hashcode. shouldn t the general hashcode contract be case specific that if you want to put this object as key in some collection that uses hashing and then overrides it??

问题回答

我要说的是,既然你不知道你(或更重要的是,另一个发展者)是否希望今后利用洗.来收集这些物品。 这可能是直接的,也可能是间接的,即,你可以把你的反对转嫁给你没有写成的另一种方法,而其他方法则在执行工作中使用shed收集方法。

It s considered good practice (or mandatory) because you can t be sure nobody else (or you next month) will reuse you class.

需求相等的物体有资格用作关键用途,例如,需要背书。

You can see that as a convention, like the indentation or the cases of parameters and class names, but that this kind of convention that helps manage the code. Somebody seeing the equals method will rightly assume there is an hashcode method (maybe in a superclass). Don t surprise him (that s a general rule).

这就是说,你在帮助图书馆制定校车方法方面有很多帮助,而且你能够从你所了解的方法中汲取灵感(例如,在你检查和确定后,可以发挥功能,产生平等器具和散射法,但大多是可操作的)。

如果你重新界定<代码>等于,则你应重新界定<代码>hashCode/,但没有必要投入太多的努力。 简言之,请看一下你喜欢的号码(8675309或不管怎样),简单地说,你们有<条码>。 这样做将确保在插入你的标语时,将正确(尽管缓慢)收集 ha。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...