import net.openhft.chronicle.map.ChronicleMap;
import java.io.File;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class App {
public static void main(String[] args) throws Exception {
Map<Point, Point> map1 = new ConcurrentHashMap<>();
ChronicleMap<Point, Point> map2 = ChronicleMap
.of(Point.class, Point.class)
.name("map")
.averageKey(new Point(10, 10))
.averageValue(new Point(10, 10))
.entries(50)
.createPersistedTo(new File("c:/temp/map/param.dat"));
Point key = new Point(12, 12);
key.hashCode();
map1.put(key, key);
map2.put(key, key);
System.out.println("ConcurrentHashMap.get returned " + map1.get(new Point(12, 12)));
System.out.println("ChronicleMap.get returned " + map2.get(new Point(12, 12)));
}
}
class Point implements Serializable {
private int x = 0;
private int y = 0;
private int _hash = 0;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public String toString() {
return super.toString() + " {" + x + "," + y + "}";
}
@Override
public int hashCode() {
_hash = 1;
_hash = _hash * 17 + x;
_hash = _hash * 31 + y;
return _hash;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Point) {
return (x == ((Point) obj).getX()) && (y == ((Point) obj).getY());
}
return false;
}
}
正如您在上面的示例中所见,ChronicleMap 的行为与 ConcurrentHashMap(与 HashMap 相同)有点不同,因为它无法使用 hashCode 或 equals 查找键。
有人可以指出可以做些什么来解决这个问题吗?
更新; 执行时,程序将返回以下结果:
ConcurrentHashMap.get returned App.Point@38f {12,12}
ChronicleMap.get returned null