哈希表不能正确计算值

时间:2018-02-01 15:17:44

标签: java hashtable

在这里,我正在尝试实现链式哈希表。在这里,当我输入相同的密钥时,它与现有密钥连接并将值加1。我得到的问题是当我打印最终表时它会给出错误的值。每次有一个键,而其他相同的键连接在一起。

插入函数编码如下。

 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);
            }        
        }


    } 

如有必要,hashTableLinked类如下所示

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”

的输入行时
We the People the the the of freedom in Order to form the

输出

Bucket 9 : the  4 
           the  1 

2 个答案:

答案 0 :(得分:1)

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

while (entry.next!= null) {

应该是

while (entry != null) {

你可以消除if语句,在循环之后处理条目为空。

        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;
        }        

答案 1 :(得分:1)

看看你的循环:

//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);
} 

这意味着当您添加相同的密钥时,您的代码将创建一个新条目,然后原始条目将在循环中更新。

你想要的是检查entry本身是否为空,而不是是否有下一个条目。

顺便说一下,通过使用调试器单步调试代码应该很容易找到。