Search code examples
javadesign-patternsequals-operator

Implementation equals() method design pattern


Is there any design pattern (or idiom) that helps to implement equals() method in Java?

That task is'n so hard but in most cases it's about the same... That's why I guess that there is a pattern but I didn't find it.

UPDATE

I chose the method: generate equals() method in Eclipse but... I found a good way (in AbstractList) to make that generated code better:

if (!(attributes ==null ? other.attributes==null : attributes.equals(other.attributes))) 
    return false; 

instead of generated:

if (attributes == null) {
        if (other.attributes != null)
        return false;
    } else if (!attributes.equals(other.attributes))
        return false;

Solution

  • Generally to implement equals() method, what I do is : I generate those from Eclipse as Eclipse can generate hashCode, toString and equals() methods very well.