I am trying to complete a task that in my hash map, I only store the KV proxy object which the actual data is storing off heap.
So I create a proxy object which override equals
and hashcode
method to make it have the same value with the origin object.
Then I test the result and find that I can not find the V again by the origin key.
ObjectOffHeapProxy offHeapKeyProxy = new ObjectOffHeapProxy(key);
System.out.println("key="+key+"---offHeapKeyProxy.hashCode()==key.hashCode() "+(offHeapKeyProxy.hashCode()==key.hashCode()));
System.out.println("key="+key+"---offHeapKeyProxy.equals(key) "+(offHeapKeyProxy.equals(key)));
ObjectOffHeapProxy offHeapValueProxy = new ObjectOffHeapProxy(value);
skeleton.put(offHeapKeyProxy,offHeapValueProxy);
//put into the map
System.out.println("put by proxy,try get object from the origin key is :" + skeleton.get(key));
//can't find it again.
System.out.println("put by proxy,try get object from the proxy is"+skeleton.get(offHeapKeyProxy));
bloomFilter.add(key.hashCode());
System.out.println("put a proxy,try get object from the proxy is"+skeleton.get(offHeapKeyProxy));
//this works but it does not meet my expectation
OUTPUT is like:
key=1---offHeapKeyProxy.hashCode()==key.hashCode() true
key=1---offHeapKeyProxy.equals(key) true
put a proxy,try get object from the origin key is :null
put a proxy,try get object from the
proxy isxxx.yyy.ObjectOffHeapProxy@b73a019f
In my opinion , the hashcode is to find the bucket position, and they will use equals
to determine if they the equals , and since hashcode are the same and they are equals(), why is that?
FYI:
public class ObjectOffHeapProxy {
private final ByteBuffer buff;
public ObjectOffHeapProxy(Object actual) throws IOException {
byte[] bytes = SerialUtil.serialize(actual);
this.buff = ByteBuffer.allocateDirect(bytes.length);
buff.put(bytes);
}
@Override
public boolean equals(Object o) {
return findActual().equals(o);
}
/**
* actual data's hashcode
* @return
*/
@Override
public int hashCode() {
return findActual().hashCode();
}
public Object findActual() {
try{
buff.flip();
byte[] target = new byte[buff.limit()];
buff.get(target);
return SerialUtil.deserialize(target);//TODO please check
}
catch (IOException | ClassNotFoundException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
}
UPDATE
Just as the comments point out, equals break things:
System.out.println("key="+key+"---offHeapKeyProxy.hashCode()==key.hashCode() "+(offHeapKeyProxy.hashCode()==key.hashCode()));
System.out.println("key="+key+"---offHeapKeyProxy.equals(key) "+(offHeapKeyProxy.equals(key)));
System.out.println("key="+key+"---key.equals(offHeapKeyProxy) "+(key.equals(offHeapKeyProxy))); //false here!!!!
But since I have no idea to make change to the key, they are from the user, how can I solve it?