Unfortunately, you cannot convince the hash set to compare basic and extended objects for equality as if they both were basic objects. However, you could build a wrapper object that holds a basic or an extended object, and uses the comparison method of the basic object to compare the two.
Here is a sketch of a possible implementation:
class FooWrapper {
private final BasicFoo obj;
public FooWrapper(BasicFoo obj) {
this.obj = obj;
}
public BasicFoo getWrapped() {
return obj;
}
public int hashCode() {
// Compute the hash code the way the BasicFoo computes it
}
public boolean equals(Object other) {
// Compare the objects the way the BasicFoo does
}
}
Now you can take a mixed collection of BasicFoo
and ExtendedFoo
, wrap them in the FooWrapper
, and put them in a hash set. You can perform operations on the sets, and then harvest the results by unwrapping the individual BasicFoo
objects from the set of wrappers.