Image

Imageilja_l wrote in Imagejava_dev

problems with HashSet

So I'm trying to write a normal Set with overloaded equals, currently I have something like this:


Set store = new HashSet();

store.add(new FooClass(1));
store.add(new FooClass(2));
store.add(new FooClass(3));
store.add(new FooClass(1));

-- FooClass contents (~) :

...
int someValue;

public FooClass(int smth) {
someValue = smth;
}
...
public boolean equals (Object o) {
FooClass tmp = (FooClass) o;
return tmp.someValue == o.someValue;
}

The problem is that all 4 objects are added to the set, though that's defintely not what I intended to achieve. Any ideas?