Dear All,
I'm missing something. Please consider the situation:
import java.util.*;
class ASet {
public static void main(
String[] args) {
Set<Drink> set = new HashSet<Drink>();
Drink one = new Drink("Soda");
Drink two = new Drink("Tonic");
Drink three = new Drink("Vodka");
set.add(one);
set.add(two);
set.add(three);
System.out.println(set); // printed {Soda, Tonic, Vodka}, no problem
one.name = "Vodka";
System.out.println(set); // printed {Tonic, Vodka, Vodka} Duplicates?
} //end main
} //end class
And Drink class overrides equals(), hashCode() and toString() this way:
class Drink {
String name;
public int hashCode() {
return name.hashCode(); // since String overrides the hashCode()
}
public boolean equals(Object drink) {
return name.equals((Drink)drink.name);
}
public String toString() {
return name;
}
}
Question is: how it's possible to have entries in a Set that look identical? Thank you.