When using HashSet or LinkedHashSet, the objects you add to them
must override hashCode(). If they don't override hashCode(), the default Object.
hashCode() method will allow multiple objects that you might consider "meaningfully
equal" to be added to your "no duplicates allowed" set.
In your case, you have not overridden hashCode method. Thus, add method will call hashCode method of Object class. Now, if that method returns different hashCode (and mostly it does) for a1 and a2, then from add method's perspective, those objects belong to two different buckets, and hence cannot be equal.