Override equals and hashcode in Java
It is not always necessary to override hashcode and equals. But if you think you need to override one, then you need to override both of them. Let's analyze what whould happen if we override one but not the other and we attempt to use a Map . Say we have a class like this and that two objects of MyClass are equal if their importantField is equal (with hashCode and equals generated by eclipse) public class MyClass { private final String importantField ; private final String anotherField ; public MyClass ( final String equalField , final String anotherField ) { this . importantField = equalField ; this . anotherField = anotherField ; } public String getEqualField () { return importantField ; } public String getAnotherField () { return anotherField ; } @Override public int hashCode () { final int prime = 31 ...