Overriding equals and hashCode values ​​on POJO with List object

I have 2 POJOs that look like this:

public class Element{
   private String name;
   private int number;
   //GETTERS AND SETTERS
}

public class Container{
    private String subject;
    private String email; 
    private List<Element> elements;
    //GETTERS AND SETTERS

}

      

And I need to check if two Container objects are the same. I looked around a bit and I found that apache commons has a HashCodeBuilder and an EqualsBuilder to help override these methods. The idea is that these construction methods use all the elements in the object to determine the HashCode and equality of the two objects. The problem is, if you see the sample code, it looks like this:

public boolean equals(Object obj) {
   if (obj == null) { return false; }
   if (obj == this) { return true; }
   if (obj.getClass() != getClass()) {
     return false;
   }
   MyClass rhs = (MyClass) obj;
   return new EqualsBuilder()
                 .appendSuper(super.equals(obj))
                 .append(field1, rhs.field1)
                 .append(field2, rhs.field2)
                 .append(field3, rhs.field3)
                 .isEquals();
  }

      

How to add List<Element> elements

? Do I need to create another method to parse the entire list to String for this? Thank!

+3


source to share


3 answers


Short version:

Yes, you can use the append

EqualsBuilder

and method HashCodeBuilder

.

Long version:

The method List.equals(Object)

compares all the elements in the list. See javadoc



Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists are the same size, and all matching element pairs in the two lists are equal. (Two elements e1 and e2 are equal if (e1 == null? E2 == null: e1.equals (e2)).) In other words, two lists are defined equal if they contain the same elements in the same order. This definition ensures that the equals method works correctly across different implementations of the List interface.

This way you can use append(elements, rhs.elements)

to compare lists.

It List.hashCode()

also uses the hash code of the items, so you can also use the append

HashCodeBuilder

. javadoc says:

Returns the hash code value for this list. The hash code of the list is determined as the result of the following calculation:

int hashCode = 1;
for (E e : list)
    hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());

      

+6


source


I believe that both Apache Commons and the IDE's auto-generated code are based on Joshua Block's recommendations in his book Effective Java .



If you are using an IDE like Eclipse, you can automatically generate equals () and hashCode () by selecting the fields you want to include in the calculations. Eclipse even allows you to use your own equals () and hashCode () or Apache Commons' collectors for that matter.

+1


source


I also have the same problem with you. I tried to generate equals (), hashCode () and toString () methods and the code works well. Here is my code:

public class EmployeeIncomeTaxRespiteDto extends AbstractDto {

private static final long serialVersionUID = 2305082424321176578L;

private Integer employeeId;

private String employeeName;

private List<IncomeTaxRespiteSelectDto> incomeTaxRespiteList;

@Override
public boolean equals(Object object) {
    return EqualsBuilder.reflectionEquals(this, object);
}

@Override
public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(this);
}

@Override
public String toString() {
    return "EmployeeIncomeTaxRespiteDto [employeeId=" + employeeId + ", employeeName=" + employeeName + ", incomeTaxRespiteList=" + incomeTaxRespiteList + "]";
}

      

}

0


source







All Articles