I have a 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 verify if two Container objects are the same. I looked a bit and I found that apache commons has HashCodeBuilder and EqualsBuilder that help overriding those methods. The idea is that those builder methods use all the elements in an Object in order to determine the HashCode and the equality of two objects. The problem is that, if you see the example 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 can I append the List<Element> elements
? Do I need to create another method just to parse the whole List into a String for this to work? Thanks!
Short version:
Yes, you can use the append
method of EqualsBuilder
and HashCodeBuilder
.
Long version:
The List.equals(Object)
method compares all the elements on the list. See the 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 have the same size, and all corresponding pairs of elements 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 to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.
So you could use append(elements, rhs.elements)
to compare the lists.
The List.hashCode()
also use the hashCode of the elements, so also can use the append
method of HashCodeBuilder
. The javadoc says:
Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:
int hashCode = 1; for (E e : list) hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With