将元素放入TreeMap中 Java

huangapple go评论72阅读模式
英文:

Putting element into TreeMap Java

问题

以下是您提供的内容的翻译:

我正在尝试使用TreeMap来存储我的数据,但是在向其中添加新元素时遇到了问题。

public class Graph {

    private TreeMap<Vertex, ArrayList<Vertex>> tree;

    public Graph() {
        this.tree = new TreeMap<>();
    }

    public void addVertex(int label) {
        this.tree.put(new Vertex(label), new ArrayList<Vertex>());
    }
}

当我使用addVertex方法时,没有出现错误,但是当我显示我的映射时,只有一个元素在我的TreeMap中:{1=[]}

public class Main {

    public static void main(String[] args) {
        Graph g = new Graph();

        g.addVertex(1);
        g.addVertex(2);
        g.addVertex(3);
        g.addVertex(4);
    }
}

我尝试创建一个基本的映射,就像这样:

TreeMap<Integer, String> map = new TreeMap<Integer, String>();
map.put(1, "dog");
map.put(2, "cat");

System.out.println(map);

这是有效的,所以我真的不明白我的错误在哪里。我还检查了我的Vertex类是否正确,它运行良好。

public class Vertex implements Comparable<Vertex> {

    private int label;

    public Vertex(int label) {
        this.label = label;
    }

    public String toString() {
        return this.label + "";
    }

    public int compareTo(Vertex o) {
        return this.label == o.label ? 1 : 0;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (! (obj instanceof Vertex)) return false;

        Vertex other = (Vertex) obj;

        return this.label == other.label;
    }

    @Override
    public int hashCode() {
        return label < 0 ? 0 : label;
    }
}

我编辑了我的Vertex类并添加了hashCode()equals(),但它仍然不起作用。

您能帮我找出我的错误吗?提前感谢您!

英文:

I'm trying to use a TreeMap to store my data, but i have a problem to add new element into it.

public class Graph {

private TreeMap&lt;Vertex, ArrayList&lt;Vertex&gt;&gt; tree;

public Graph() {
    this.tree = new TreeMap&lt;&gt;();
}

public void addVertex(int label) {
    this.tree.put(new Vertex(label), new ArrayList&lt;Vertex&gt;());
}

When I use my addVertex method, I don't have error but when I display my map, I only have 1 element into my treemap : {1=[]}

public class Main {

public static void main(String[] args) {
  Graph g = new Graph();


  g.addVertex(1);
  g.addVertex(2);
  g.addVertex(3);
  g.addVertex(4);

I tryed to create a basic map like that :

TreeMap&lt;Integer, String&gt; map = new TreeMap&lt;Integer, String&gt;();
  map.put(1, &quot;dog&quot;);
  map.put(2, &quot;cat&quot;);

  System.out.println(map);

And it works, so I really don't understand my mistake.
I also check if my class Vertex was correct, and it is working well.

public class Vertex implements Comparable&lt;Vertex&gt; {

private int label;

public Vertex(int label) {
    this.label = label;
}

public String toString() {
    return this.label + &quot;&quot;;
}

public int compareTo(Vertex o) {
    return this.label == o.label ? 1 : 0;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) return false;

    if( ! (obj instanceof Vertex) ) return false;

    Vertex other = (Vertex) obj;

    return this.label == other.label;
}

@Override
public int hashCode() {
    return label &lt; 0 ? 0 : label;
}

}

I edited my Vertex class and addded hashcode() and equals() but it still doesn't work.

Can you help me to find my mistake please ?
Thanks by advance !

答案1

得分: 1

你的 compareTo 方法是错误的。查看一下其规范

> 实现者必须确保对于所有的 x 和 y,sgn(x.compareTo(y)) == -sgn(y.compareTo(x))。

由于你只返回值 1 和 0,这个条件无法满足。

一般来说,一个可比较的对象必须始终支持以下三种情况:

  • this 小于参数:返回一个负整数(通常为 -1
  • this 与参数排序相同:返回 0
  • this 大于参数:返回一个正整数(通常为 1

如果你的代码没有涵盖这三种情况,那就不正确。

由于你基本上想根据 label 进行排序,最简单的实现方法是委托给 Integer.compare

public int compareTo(Vertex o) {
    return Integer.compare(this.label, o.label);
}
英文:

Your compareTo method is wrong. Take a look at its specification:

> The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y.

Since you only ever return the values 1 and 0, this condition can not hold.

In general a comparable must always support all three cases:

  • this is less than the argument: return a negative integer (usually -1)
  • this is sorted the same as the argument: return 0
  • this is greater than the argument: return a positive integer (usually 1)

If your code doesn't have those three cases, it can't be correct.

Since you basically want to sort according to label, the simplest implementation would be to delegate to Integer.compare:

public int compareTo(Vertex o) {
    return Integer.compare(this.label, o.label);
}

huangapple
  • 本文由 发表于 2020年10月6日 17:04:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64222603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定