I was asked a problem - There are some values in database. And we need to add these values for some class. At some point these values get some hashcode that got repeated for different values for example i m having MCFJ and RTYZ and they are having say 10 for this hashcode and we will add values to the class based on this hashcode. What implementation should we give to stop repetiotion of hashcode at some time.
I think answer could be no implementation of the hashcode or we may implement it like giving some value to M, C and blah blah and use xor operator.
Would anyone like to give input on this?
If you do not override it, then the hashCode will return a value generated out of converting the internal address of the object into an integer. Therefore we could expect that all objects have a different hash code (Not sure what would happen, though, if the number of active Objects surpass Integer.MAX_VALUE).
Now, if you decide to override the method yourself, then you will need to make sure that for a given class, the hashCode you generate is as unique as possible. Typically you do this using an algorithm that has as fewer collisions as possible, if any. At any rate, the Java contract for hashCode() does not require that two give objects have different hashCodes all the time. It does require that if two objects are considered equal by the equals() method that they should have the same hash code value.
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer,
provided no information used in equals comparisons on the object is modified.
Therefore, regardless of the implementation of garbage collection or any other mechanisms that might place the object in another memory space, this method ought to return the same integer during the execution of the JVM.
Now, that being said, you must consider that this behavior of returning the memory space of an object as a hash code is not a required implementation of the method. Other JVM might implement it differently without violating the contract. Therefore, I would suggest that you should not take this implementation-dependent details into account while dealing with hashCode implementations.
You should only consider the contract stated in javadoc.
The question is - We want to use hashcode() to add element for a class. There is no equals() method. But at some instance hashcode() has same value for different pattern string. Say UFWX has hashcode() - 10 and FWXV - 10. So Interviewer wanted to ask me what could be the best implementation in that case for hashcode. He wanted to use hashcode to decide the duplication of records. UFWX and FWXV are two different values but they have same hashcode. I could tell only this - we could use XOR to manage the things.
Please suggest or let me know if I am not clear all again
Thanks & Regards,
So when you say about the interviewer:
He wanted to use hashcode to decide the duplication of records.
The answer to that is: No. The equals() method is the method you use for that.