I have a TreeSet of objects called Artifacts. I have overridden the equals and hash code methods in the object like so:
@Override
public int hashCode() {
return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers
// if deriving: appendSuper(super.hashCode()).
append(artifactId).
toHashCode();
}
@Override
public boolean equals(Object arg0) {
Artifact obj=(Artifact)arg0;
if(this.getArtifactId().equalsIgnoreCase(obj.getArtifactId()))
{
return true;
}
return false;
}
I have put prints in the equals method and it is never called. I have instantiated the TreeSet with a comparator which looks like:
TreeSet<Artifact> syncedList = new TreeSet<Artifact>(new ArtifactComparator());
I have read that the TreeSet establishes it's uniqueness based on the equals override.
I see multiple objects with the same ArtifactId in the TreeSet which is not unique like I need.
Is something missing in my equals and hash code methods?