HashSet and HashCode reminder

When working on recent project, here is a common mistake I encountered with an equivalent code snitbit
If HashSet does not allow duplicates, why do the following two objets (w1 and w2 return true)?


public static void main(String[] args) {
Set<Widget> widgets = new HashSet<Widget>();
Widget w1 = new Widget("a");
Widget  w2 = new Widget("a");
Iterator it = widgets.iterator();
while(it.hasNext())  {
System.out.println(it.next() );
System.out.println(w1.equals(w2) );  // true
}
</span>
public class Widget{
 String name;
Widget(String s){
   this.name = s;
}

public String toString(){
   return this.name;
}
public boolean equals(Object o){
if((o instanceof Widget) && (this.name.equals(((Widget)o).name)))
return true;
else
return false;
}

}

Issue

According to the Java specification, whenever you override equals() you need to override hashCode() too.
HashSet uses both hashCode() first to find the right bucket, but then it still needs to use equals(), because the hashcode is allowed to be ambigous.
It does this because it needs the hash code (a number representing the object) to determine where in the set to store that Object.
However in the case of TreeSet, it depends on the return value of compareTo (or compare) methods. If the return value is 0, it won’t allow one to do an insertion.

Published by anthonykuong

Anthony is a versatile Software professional with around 10 years of experience. He is a Full Stack developer experienced with clients in the Financial, Health and Supply Chain industries. He is experienced with MVC frameworks ( Spring Boot) , SPA frameworks ( Angular , VueJS), and also supports automated build deployments and packaging for development, qa, and production servers.. He has delivered rich user experience using Modern web technologies and techniques such are HTML5, CSS3, ECMAScript 6 (ES6)/ ECMAScript 2015, CSS pre-processors (SASS, Less), JavaScript build tools (Grunt, Gulp) , various UI Frameworks including AngularJS , Knockout JS , and CSS Frameworks including Bootstrap, and Foundation. He is adaptable to new technologies and frameworks. He is a rigorous, quality-conscious contributor with solid analytical skills. I can also be found on youtube - Youtube Channel: https://www.youtube.com/user/akuong/

Leave a comment