The default equals() from Object returns true only when both references point to the exact same object. For your own classes, you usually want equals() to compare field values instead.
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Dog)) return false;
Dog other = (Dog) o;
return this.name.equals(other.name)
&& this.age == other.age;
}
Whenever you override equals(), you must also override hashCode(). Collections like HashMap and HashSet use hashCode() to locate objects. If objects are equal according to equals(), they must return the same hashCode(). Breaking this contract causes objects to "disappear" from hash-based collections.
@Override
public int hashCode() {
return Objects.hash(name, age);
}