I have a somewhat niche need to key off two objects of generic types X
and Y
, and use that to return type T
. I want to put these items in tightly managed HashMap
and use that to look up on the X,Y key.
However, how do I ensure X
and Y
are hashcoded and equality compared on their memory reference, and not any overridden implementation of hashCode/equals?
public final class BiReferenceCache<X,Y,T> {
private final HashMap<Key<X,Y>,T> cacheMap = new HashMap<>();
public T get(X item1, Y item2) {
return cacheMap.get(new Key<X,Y>());
}
private static final class Key<X,Y> {
private final X val1;
private final Y val2;
Key(X val1, Y val2) {
this.val1 = val1;
this.val2 = val2;
}
public int hashCode() {
//??? What do I do to hashcode by val1 and val2's memory reference?
}
public boolean equals(Object obj) {
//??? What do I do to check equality for val1 and val2's memory reference?
}
}
}