Java 哈希表未正确计算值

Java 哈希表未正确计算值,java,hashtable,Java,Hashtable,在这里,我试图实现一个链式哈希表。在这里,当我输入同一个键时,它将与现有键联接,并向值中添加一个键。我遇到的问题是,当我打印最终表格时,它给出了错误的值。每次都有一个键,而其他相同的键连接在一起 插入函数的编码如下 public void insert (String key, int value){ int hashValue= generateHashValue(key); //find what bucket suits for each key i

在这里,我试图实现一个链式哈希表。在这里,当我输入同一个键时,它将与现有键联接,并向值中添加一个键。我遇到的问题是,当我打印最终表格时,它给出了错误的值。每次都有一个键,而其他相同的键连接在一起

插入函数的编码如下

 public void insert (String key, int value){

        int hashValue= generateHashValue(key); //find what bucket suits for each key


        if(table[hashValue] == null) {
            table[hashValue] = new HashTableLinked(key, value);//enter new key           
        }

        else{
            HashTableLinked entry = table[hashValue];
            boolean condition =false;

            while (entry.next!= null){

                if (entry.getKey().equals(key)) {

                    entry.setValue(entry.getValue()+1);  //trying to add +1 for existing key 
                    condition = true;
                    break;
                }

                entry = entry.next;
            }
            if(!condition) {
                entry.next = new HashTableLinked(key, value);
            }        
        }


    } 
如有必要,HashTableLink类如下所示

public class HashTableLinked {

   private String key;
     int value;
     HashTableLinked next;

   public HashTableLinked(String key,int value){
     this.key = key;
     this.value = value;
     this.next = null;

   public void setValue(int value) {
     this.value = value;
   }

   public String getKey() {
      return key;
   }
   public int getValue(){
     return value;
  }
}
当我输入包含5个“the”的输入行时

输出是

Bucket 9 : the  4 
           the  1 

尽管已在if语句中进行了检查:

while (entry.next!= null) {
应该是

while (entry != null) {
您可以消除if语句,注意循环后的条目为null

        HashTableLinked entry = table[hashValue];
        boolean found = false;
        HashTableLinked priorEntry = null;
        while (entry != null) {
            if (entry.getKey().equals(key)) {
                entry.setValue(entry.getValue() + 1);  //trying to add +1 for existing key 
                found = true;
                break;
            }
            priorEntry = entry;
            entry = entry.next;
        }
        if (!found) {
            if (priorEntry == null) {
                table[hashValue] = new HashTableLinked(key, value);
            } else {
                priorEntry.next = new HashTableLinked(key, value);
            }
        }        
确实很尴尬。最好在前面插入:

        boolean found = false;
        for (HashTableLinked entry = table[hashValue]; entry != null; entry = entry.next) {
            if (entry.getKey().equals(key)) {
                entry.setValue(entry.getValue() + 1);  // trying to add 1 for existing key 
                found = true;
                break;
            }
        }
        if (!found) {
            HashTableLinked added = new HashTableLinked(key, value);
            added.next = table[hashValue];
            table[hashValue] = added;
        }        

看看你的循环:

//This will be the first entry for that bucket
HashTableLinked entry = table[hashValue];
boolean condition =false;

//What happens when the bucket only contains one entry? The loop won't get executed
while (entry.next!= null){
  if (entry.getKey().equals(key)) {
    entry.setValue(entry.getValue()+1);  //trying to add +1 for existing key 
    condition = true;
    break;
  }
  entry = entry.next;
}

//If the loop doesn't get executed, condition will be false
if(!condition) {
  entry.next = new HashTableLinked(key, value);
} 
这意味着,当您添加一个相同的键时,您的代码将创建一个新条目,从那时起,原始条目将在循环中更新

        HashTableLinked entry = table[hashValue];
        boolean found = false;
        HashTableLinked priorEntry = null;
        while (entry != null) {
            if (entry.getKey().equals(key)) {
                entry.setValue(entry.getValue() + 1);  //trying to add +1 for existing key 
                found = true;
                break;
            }
            priorEntry = entry;
            entry = entry.next;
        }
        if (!found) {
            if (priorEntry == null) {
                table[hashValue] = new HashTableLinked(key, value);
            } else {
                priorEntry.next = new HashTableLinked(key, value);
            }
        }        
您要检查的是
条目本身是否为空,而不是是否有下一个条目


顺便说一句,通过使用调试器单步执行代码,应该很容易发现这一点。

您调试了代码吗?是否有理由实现自己的哈希表而不是使用Java的哈希表?这是一个家庭作业吗?抱歉,但是如果没有足够的代码运行,很难猜测问题出在哪里,例如这里没有描述方法generateHashValue(key)。这同样适用于{table}属性。我认为代码的另一部分是不必要的。。因为它包含更多的行。@Kamil…这是一种家庭作业。。但它不是你认为的家庭作业:)@JPRLCol@roch.p仔细阅读:当您第三次添加密钥时,循环将执行。第一次进入
if(table[hashValue]==null)
分支时,第二次
while(entry.next!=null)
不会像
entry那样进行一次迭代。next
将立即为null。这就是为什么会有两个条目,以及为什么除了一个(第二个)之外的所有计数都会添加到第一个条目。无需事先输入就可以找到后面要添加的最后一个元素。非常感谢。它起作用了。。为什么插入前端总是更好?减少时间?