Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何防止图形添加多个自定义边?_Java_Vertex_Edges_Jung - Fatal编程技术网

Java 如何防止图形添加多个自定义边?

Java 如何防止图形添加多个自定义边?,java,vertex,edges,jung,Java,Vertex,Edges,Jung,我定义了一个自定义边和顶点类型,用于无向稀疏图。问题是图形添加了我不想要的多条边。例如,考虑到以下代码: UndirectedSparseGraph<Vertex, Edge> graphX = new UndirectedSparseGraph<Vertex, Edge>(); graphX.addEdge(new Edge("1#2"), new Vertex("1"), new Vertex("2")); graphX.addEdge(new Edge("1#2")

我定义了一个自定义边和顶点类型,用于无向稀疏图。问题是图形添加了我不想要的多条边。例如,考虑到以下代码:

UndirectedSparseGraph<Vertex, Edge> graphX = new UndirectedSparseGraph<Vertex, Edge>();
graphX.addEdge(new Edge("1#2"), new Vertex("1"), new Vertex("2"));
graphX.addEdge(new Edge("1#2"), new Vertex("1"), new Vertex("2"));
graphX.addEdge(new Edge("2#1"), new Vertex("2"), new Vertex("1"));
graphX.addEdge(new Edge("1#3"), new Vertex("1"), new Vertex("3"));
graphX.addEdge(new Edge("1#4"), new Vertex("1"), new Vertex("4"));
那么,我做错了什么

仅供参考以下是我创建的课程:

public class Vertex {

    private String id;
    //More info in the future

    public Vertex(String id){
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object obj){
        return ((Vertex) obj).id.equals(this.id);
    }

    @Override
    public String toString(){
        return this.id;
    }

}

public class Edge {

    private String id;
    private double weight;

    public Edge(String id, double weight){
        this.id = id;
        this.weight = weight;
    }

    public Edge(String id){
        this.id = id;
        this.weight = -1;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    @Override
    public boolean equals(Object obj){
        return ((Edge) obj).id.equals(this.id);
    }

    @Override
    public String toString(){
        return this.id;
    }

}

这是一个典型的Java问题,不是JUNG特有的。基本上,您覆盖了equals(),但没有覆盖hashCode(),因此您的hashCode“与equals()不一致”。有关更多上下文和一些解决方案,请参见此问题及其答案:

replice of
public class Vertex {

    private String id;
    //More info in the future

    public Vertex(String id){
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object obj){
        return ((Vertex) obj).id.equals(this.id);
    }

    @Override
    public String toString(){
        return this.id;
    }

}

public class Edge {

    private String id;
    private double weight;

    public Edge(String id, double weight){
        this.id = id;
        this.weight = weight;
    }

    public Edge(String id){
        this.id = id;
        this.weight = -1;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    @Override
    public boolean equals(Object obj){
        return ((Edge) obj).id.equals(this.id);
    }

    @Override
    public String toString(){
        return this.id;
    }

}