Fix that Java: Set is Bigger
As we see, the following code works bit different as we expect.
import java.util.Set;
import java.util.HashSet;
class Main {
public static void main(String[] args) {
Set<Dog> dogs = new HashSet<>();
Dog caesar = new Dog("German Shepherd");
Dog rex = new Dog("German Shepherd");
dogs.add(caesar);
dogs.add(rex);
System.out.println(caesar.equals(rex));
System.out.println(dogs.size());
}
}
class Dog {
private String breed;
public Dog(String breed) {
this.breed = breed;
}
public int hashCode() {
return breed.hashCode();
}
public boolean equals(Dog o) {
return breed.equals(o.breed);
}
}
Fix it and enjoy!