As per Joshua Bloch Effective Java Second Edition, Item 9 Always override hashCode when you override equals.
“You must override hashCode in every class that overrides equals. Failure to do so
will result in a violation of the general contract for Object.hashCode, which will
prevent your class from functioning properly in conjunction with all hash-based
collections, including HashMap, HashSet, and Hashtable.”
Another Important Line from the Effective Java “In hashCode method you must exclude any fields that are not used in equals comparisons,
or you risk violating the second provision of the hashCode contract”(To refer to contract look into Object.java (javadoc))
I want to add to this and say :
Always compareTo method should reflect same logic as you have in equals. Continue reading »

Recent Comments