I want to use java.util.Set (and other collections) but with a twist: I want contains() , add(), etc. to call Object’s equals() at all times (that is operate based on identity rather than equality more generally). I think I have a way, but it has big drawbacks. Is there a proper way to do this? Sorry if I’m missing something obvious.
Here’s what I’ve done:
public class OnlySelfEqual {
public final boolean equals(Object o){
return super.equals(o);
}
}
public class Example{
private Set<T extends OnlySelfEqual> set;
//etc
}
The main problem I see with this (there may be numerous others) is that all Ts have to extend from a class rather than implement an interface, which is pretty restrictive. I think what I want would be something like a ‘reverse’ interface which lists methods that subtypes cannot implement (override). I’m pretty sure that doesn’t exist though. Any suggestions?
java.util.IdentityHashMapis aMapimplementation that deliberately violates theMapcontract by using==rather thanequals(), so you could get aSetwith the same behaviour using e.g.