Using composition, you can create a FooWrapper class that provides a custom implementation of equals and instead adds this to the set:
public class FooWrapper { public final Foo foo;
Unfortunately, with the Java Collections API, there is no way to tell the collection to use a custom equals calculation other than the method above or a subclass and override of equals() .
Edit:
It seems to me that instead you can use Map<Integer, Foo> instead, using foo.A as the key and foo as the value (and thereby restricting it to unique A values). I could not tell you if this is suitable without additional context.
Mark peters
source share