简体   繁体   English

Java TreeMap实现的get和put方法

[英]Java TreeMap implementation get and put methods

I'm writing an implementation of TreeMap, and am having trouble with the get and put methods. 我正在编写TreeMap的实现,并且在get和put方法方面遇到麻烦。 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;

private V get(K searchKey) {
    if(this.isEmpty())
        return null;//it needs an exception

    if(this.key.compareTo(searchKey) == 0)
        return this.value;
    else if(this.key.compareTo(searchKey) > 0)
        return this.left.get(searchKey);
    else
        return this.right.get(searchKey);
}

public V put(K key, V value) {

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

        else if(this.key.compareTo(key) < 0)
            return this.right.put(key, value);
        else if(this.key.compareTo(key) > 0)
            return this.left.put(key, value);
    }

    else {
        if(this.isLeaf() || this.isEmpty()) {
            if(this.key.compareTo(key) > 0) //this line gives NPE during tests
                this.left = new MyTreeMap(key,value,null,null);
            else
                this.right = new MyTreeMap(key,value,null,null);

               //check for balance and rebalance if needed
            this.size++;
            this.setHeight();
            return null;
        }

        else {
            if(this.key.compareTo(key) > 0)
                return this.left.put(key, value);
            else
                return this.right.put(key, value);
        }
    }
}

The craziest error is that the put method requires another return statement. 最疯狂的错误是put方法需要另一个return语句。 Checking through the code a bunch of times, it seems to me that this should not be the case, as there is a return statement that does not require any boolean statement to be true. 在代码中检查了很多遍,在我看来,情况并非如此,因为有一个return语句不需要任何布尔语句为真。

While testing the put method, I get an NPE. 在测试put方法时,我得到了NPE。 I think there are some pretty significant logic errors with my code, because I cannot seem to figure out what is wrong. 我认为我的代码存在一些非常重要的逻辑错误,因为我似乎无法弄清楚什么是错误的。 If you could please point me in the right direction to fix these various errors, that would be helpful. 如果可以的话,请向我指出正确的方向来修复这些各种错误,这将对您有所帮助。 Thank you. 谢谢。

About the "extra" return statement: 关于“额外” return语句:

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

    else if(this.key.compareTo(key) < 0)
        return this.right.put(key, value);
    else if(this.key.compareTo(key) > 0)
        return this.left.put(key, value);
}

Your logic is that you are checking this.key.compareTo(key) against <0 , >0 and ==0 so you have all your cases covered. 您的逻辑是要针对<0>0==0检查this.key.compareTo(key) ,以便涵盖所有情况。 But this is not the case for the compiler since: 但这不是编译器的情况,因为:

  1. The compiler does not know if the value of this.key.compareTo(key) is the same in all three executions. 编译器不知道this.key.compareTo(key)的值在所有三个执行中是否都相同。 Even if it had the intelligence of checking the method and seeing that it does not use any other input to get the result (it does not), the compiler has no way of knowing if another thread is concurrently changing the values of the keys. 即使它具有检查方法的智能,并且看到它不使用任何其他输入来获取结果(没有),编译器也无法知道另一个线程是否正在同时更改键的值。

  2. Even if you do int value=this.key.compareTo(key) and later perform the checks against value , the compiler does not check that the successive if-elsif cover all the ranges of values. 即使您进行int value=this.key.compareTo(key)并随后对value进行检查,编译器也不会检查连续的if-elsif是否覆盖所有值范围。 Anyway, for performance / concurrency reasons, I suggest you use this approach to only call compareTo once. 无论如何,出于性能/并发性的原因,建议您使用这种方法只调用一次compareTo

The easiest fix would be just changing the last else if (this.key.compareTo(key) > 0) for just else (as you should know that if that block is executed is because the if must be true. 最简单的解决方法是将else if (this.key.compareTo(key) > 0) else (因为您应该知道是否执行了该块是因为if必须为true)。

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

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