I passed this exercise without implementing the equals() and hashCode() methods.
I simply overrode the clone() method of the Solution class in which I cloned the "users" map of the original object to the cloned object's "users" field.
In the "right solution" the equals() and hashCode() methods are implemented. Why? I can't see where they are used in the code and why they are needed.
Am I missing something or was creating the equals() and hashCode() just extra Conditions which had nothing to do with the cloning process?
Thanks in advance.
Why use euals() and hashCode() ?
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Mike McKenna
16 June 2021, 14:41
Hi Jurij
It is difficult to give a answer without seeing Ur code.
According to task conditions :
The Solution class should support the Cloneable interface.
The User class should support the Cloneable interface.
The User class must correctly implement the clone method.
The Solution class must correctly implement the clone method.
no where in the conditions does it mention using equals() or Hash code ().
in help section :
Deep cloning means cloning each and every object that is present in an instance of object u r cloning
In other words you need to also clone every object in your map.
The right solution is a great resource .
Mike
+1
Jurij Thmsn
17 June 2021, 08:24
Hey mike, thanks fo your answer! My code works like this (I passed):
The User and Solution classes implement Cloneable interface. Except of this the only things I changed were:
I didn't implement the equals() and hashCode() methods. In the Tasks' condition it is stated:
"Provide the ability to create a deep clone of a Solution object.
The data in the users map should also be cloned.
Don't forget about the equals and hashCode methods needed to correctly add User elements to the HashMap."
Even though it is not written in the requirements. In the right solution equals() and hachCode() are implemented. I am just confused if its just an extra requirement which the validation doesn't check for or if it is needed for the cloning process somehow.
0