简体   繁体   English

TreeMap实现中的Put方法

[英]Put method in an implementation of TreeMap

I'm implementing a TreeMap class called MyTreeMap, and the put method is giving me some trouble. 我正在实现一个名为MyTreeMap的TreeMap类,而put方法给我带来了一些麻烦。 During testing, instead of updating the value of a key that is already there, it just seems to clear the node entirely. 在测试过程中,似乎没有完全更新节点,而没有更新已经存在的键的值。 Here is the code: 这是代码:

public class MyTreeMap<K extends Comparable<? super K>,V> extends AbstractMap<K,V>  {

K key;
V value;
int height;
MyTreeMap<K,V> left,right;
int size;

public V put(K key, V value) {

    if(this.isEmpty()) {
        this.key = key;
        this.value = value;

        this.size++;
        setHeight();

        return null;
    }

    else if(this.key.compareTo(key) == 0) {
        V temp = this.value;
        this.value = value;
        return temp;
    }

    else if(this.key.compareTo(key) > 0) {
        if(this.left == null) {
            this.left = new MyTreeMap<K,V>(key,value,null,null);
            this.size++;
            if(left.height > right.height + 1 || right.height > left.height + 1)
                restructure(this);
            setHeight();
            return null;
        }
        else
            return this.left.put(key, value);
    }
    else {
        if(this.right == null) {
        this.right = new MyTreeMap<K,V>(key,value,null,null);
        this.size++;
        if(left.height > right.height + 1 || right.height > left.height + 1)
            restructure(this);
        setHeight();    
        return null;
        }
        else
            return this.right.put(key, value);
    }
}

Here is the test, the first assertEquals passed, the second one did not, the failure trace is shown at a comment in that line 这是测试,第一个assertEquals通过,第二个未通过,失败跟踪显示在该行的注释中

@Test
public void putTest2() {
    TreeMap<String,LinkedList<Integer>> actual = new TreeMap<String,LinkedList<Integer>>();
    MyTreeMap<String,LinkedList<Integer>> test = new MyTreeMap<String,LinkedList<Integer>>();

    LinkedList<Integer> actualList = new LinkedList<Integer>();
    actualList.add(0);
    actualList.add(4);

    LinkedList<Integer> testList = new LinkedList<Integer>();
    testList.add(0);
    testList.add(4);

    actual.put("hello", actualList);
    test.put("hello", actualList);

    assertEquals(actual, test); //this part passes, indicating that it adds new keys correctly

    LinkedList<Integer> tempList;

    tempList = actual.get("hello");

    tempList.add(6);

    actual.put("hello", tempList);
    test.put("hello", tempList);

    assertEquals(actual, test); //this part fails, fail trace: expected:<{hello=[0,4,6,6]}> but was <[]>
}

} }

If you could give me any help on solving this bug, that would be helpful. 如果您能为我提供解决此错误的任何帮助,那将是有帮助的。 Thanks. 谢谢。

In this case, assertEquals(a, b) tests if the two Map parameters are the same object , not that they contain the same values . 在这种情况下, assertEquals(a, b)测试两个Map参数是否是同一对象 ,而不是它们包含相同的value

Neither your class nor TreeMap implement equals() so the default implementation from the Object class is used, which simply returns a == b . 您的类和TreeMap都没有实现equals()因此使用了Object类的默认实现,它仅返回a == b

Have a look at the Hamcrest library for meaningfully comparing collections by value. 查看Hamcrest库,以按价值有意义地比较集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM