如何正确使用双坐标作为HashMap中的键?

我有一个定义节点的类(一个有三个双坐标的点).

public class Node {
    private final double x,y,z;

    public Node() {
    }

    public Node(final double x,final double y,final double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public void setCoordinates(final double x,final double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
}

我需要创建很多节点并给它们整数ID,但我必须避免重复.

创建节点的方法如下所示:

private Node vertex = new Node(0,0);
private final AtomicInteger nodeCounter = new AtomicInteger();
private final Map<Node,Integer> nodeList = new HashMap<>();

public int addNode(final double x,final double z) {
    vertex.setCoordinates(x,z);

    int nodeId;

    if(nodeList.get(vertex) == null) {
        nodeId = nodeCounter.incrementAndGet();
        nodeList.put(new Node(x,z),nodeId);
    } else {
        nodeId = nodeList.get(vertex);
    }

    return  nodeId;
}

当然,这不起作用,因为HashMap的getfunction总是返回null.

所以我想我需要覆盖Node类中的hashCode方法.

我已经看过here如何为单个double做这个,但我不知道如何创建一个考虑节点的三个坐标的哈希函数.

我是否还必须覆盖equals函数?或者hashCode函数是否足够?

解决方法

So I guess I need to override the hashCode method in the Node class.

这只是交易的一部分.您还需要覆盖equals,以使您的类作为哈希映射的键工作:

@Override
public int hashCode() {
    return 31*31*Double.valueOf(x).hashCode()
          +   31*Double.valueOf(y).hashCode()
          +      Double.valueOf(z).hashCode();
}
@Override
public boolean equals(Object other) {
    if (!(other instanceof Node)) {
        return false;
    }
    Node n = (Node)other;
    return x == n.x && y == n.y && z == n.z;
}

在Java 8中,Double.valueOf(x).hashCode()应替换为Double.hashCode(x)以避免不必要的装箱:

@Override
public int hashCode() {
    return 31*31*Double.hashCode(x)
          +   31*Double.hashCode(y)
          +      Double.hashCode(z);
}

Do I also have to override the equals function? Or is the hashCode function enough?

是的,您必须始终使用hashCode(why?)覆盖equals

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...