覆盖 Graphstream 的 hashCode()

Override hashCode() of Graphstream

如何覆盖 Graphstream 节点对象的 hashCode()?

我正在将节点插入到哈希集中。

HashSet<Node> set = new HashSet<Node>();

您可以继承 Node 以使用 Objects.hash(Object...) 扩展此 class 中的对象散列。把你想要散列在一起的任何东西都扔进这个方法...

class MyNode extends Node {

// whatever floats you boat here

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), other);
    }

    @Override
    public boolean equals(Object o) {
    // hashCode() and equals() overrides should always appear together
    }
}

也许看看here

编辑:不要忘记 equals() 覆盖!