Why always override hashcode() if overriding equals()? Give me an example..
Asked
Active
Viewed 115 times
1
-
4[Click me gently](http://www.javaworld.com/article/2074996/hashcode-and-equals-method-in-java-object---a-pragmatic-concept.html). – Maroun Jan 07 '14 at 10:25
-
1They are not interchangeable, which renders your question senseless. It's like asking "what is the difference between `String` and a normal `List`"? – Marko Topolnik Jan 07 '14 at 10:27
-
http://stackoverflow.com/questions/17027777/relationship-between-hashcode-and-equals-method-in-java – ATR Jan 07 '14 at 10:29
1 Answers
1
There is only one equals()
and hashcode()
method in Object class. There is no normal or not normal version of methods.
If you are using any hash related data structure like HashMap or HashSet then you need to override hashcode() method. However in general it is a good practice to override hashcode() whenever you override equals().

Aniket Thakur
- 66,731
- 38
- 279
- 289