I need Set collection, where its items will be identified by items class. Something like ReferenceIdentityMap
from Appache Collections, but on class scope i.e. two different instances of same class must be identified as same in this collection.
You know, it is a violation of equals()/hashCode()
identity principle but in occasional use it makes sense.
I have done this in simple class backing with Map<Class<? extends E>, E>
, but due to simplicity it doesn't implement Set<E>
. There may be a more elegant solution, decorator of any Set<E>
would be great.
Is there any implementation of such collection there (Apache/Google/something/... Collections)?
You wish to override the meaning of equals() / hashCode() for your set members. The cleanest way to do this, I imagine, is to use a wrapper class:
class Wrapper<E> {
private final E item;
Wrapper(E item) {
this.item = item;
}
E getItem() {
return item;
}
public boolean equals(Object o) {
if (!(o instanceof Wrapper)) {
return false;
}
return getClass().equals(o.getClass());
}
public int hashCode() {
return getClass().hashCode();
}
}
You would create a Set<Wrapper<E>>
then.