Skip to main content

Entity Equals

Posted by evanx on June 28, 2007 at 8:01 AM PDT

Let's look at neatening up those messy equals() and hashCode() methods in our entities.

Entity Equals: A part of the "Jelly Beans" part of a trilogy in 42 parts"

public class Feed {
    private int id;
    private String uri;
    private Comparable[] values() {
        return new Comparable[] {id, uri};
    public boolean equals(Object object)  {
        if (object instanceof Feed) {
            Feed feed = (Feed) object;
            return typeHelper.equals(values(), feed.values());
        return false;
    public int hashCode() {
        return typeHelper.hashCode(values());

where we implement a values() method to return an array of our IDs et al for hashCode() and equals(), and utilise a helper class.

Resources - where i will collate these articles and their code.