I don\'t usually code in Java, but recently I started not having a choice. I might have some major misunderstanding of how to properly use HashSet. So it might be possible s
You're not actually overriding Object.equals
; instead, you're defining a new method with the same name but different parameters. Notice that Object.equals
takes an Object
argument, while your equals method takes an L
argument. If you rewrite your equals method to take an Object
and perform the necessary type-checking/casting to L
at runtime, then your code is work as you expect.
Also, this is why you really should use @Override annotations whenever your JRE supports them. That way, the compiler will complain if you accidentally implement a new method when you intend to override an existing one.
By way of an example, this equals method should work correctly. (And, on an unrelated note, it won't fail if the object being compared to is null.)
@Override
public boolean equals(Object other) {
return other != null && other instanceof L && this.l == ((L)other).l;
}