I know that I should implement hashCode()
and equals()
, however in reality I have no idea how to do it. I have a following class:
public class Dog {
private Integer id;
private Integer age;
private Instant dateOfBirth;
}
And now, I would like to be able to check if some list of dogs is subset of another list of dogs.
ArrayList<Dog>
Two dogs I consider as equals when they have the same age
, id
, dateOfBirth
Ofcourse, it seems to be easy to implement equals:
public boolean equals(Dog dog) {
return (dog.id == this.id && dog.age == dog.age && &this.dateOfBirth.equals(dog.dateOfBirth));
}
However, I have no idea how to implement hashCode
to maintain equals-hashcode contract ?