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查找关键字。为什么ChronicleMap不使用hashCode或等于查找键?
有人可以指出可以做些什么来解决这个问题吗?
UPDATE; 当被执行时,该程序将返回以下结果:
ConcurrentHashMap.get returned [email protected] {12,12}
ChronicleMap.get returned null
*无关:*为什么'_hash'是一个字段(又名实例变量)?它应该只是一个*局部变量*。 – Andreas
*“正如你在上面的例子中看到的,ChronicleMap的行为有点不同”*我必须失明,因为我看不到这一点。 *问题中的哪些地方我们会看到?我们应该在代码的输出中看到吗?如果是这样,编辑问题并显示输出,否则我们看不到它。 – Andreas
我已经更新了执行结果的例子。 – helloWorld998