Java哈希表搜索函数

Java哈希表搜索函数,java,hashtable,Java,Hashtable,我创建了一个哈希表,但我似乎被一个问题卡住了。我在哈希表中有数据,当搜索数据时,它会按预期返回。但是,如果我搜索的内容不在表中,但仍然散列到存在的元素,它不会返回false 例如:我将Hello作为哈希表中的一个键,比如说元素15。然后我搜索World,它的哈希值与Hello相同,举个例子 我希望我的代码返回null,因为即使密钥散列相同,它们也不相等。但是我下面的代码将返回Hello的键/数据(记录) @SuppressWarnings("rawtypes") public Record s

我创建了一个哈希表,但我似乎被一个问题卡住了。我在哈希表中有数据,当搜索数据时,它会按预期返回。但是,如果我搜索的内容不在表中,但仍然散列到存在的元素,它不会返回false

例如:我将Hello作为哈希表中的一个键,比如说元素15。然后我搜索World,它的哈希值与Hello相同,举个例子

我希望我的代码返回null,因为即使密钥散列相同,它们也不相等。但是我下面的代码将返回Hello的键/数据(记录)

 @SuppressWarnings("rawtypes")
public Record search(T k) {
    int i = hash(k);//Assign the computed hash value (combination of Record Class hashCode and Table's hash function above) to i.
    if (a[i] == null || a[i].record.equals(k)) { 
        return null; 
    } else if (!a[i].record.equals(i) && a[i].record.getKey() != k) {//otherwise, the record is found and if the key stored does not equal the key being searched return null
        return a[i].record;
    } else { //otherwise the record is not the first record in the linked list
        cursor = a[i]; //set cursor to equal the entire list of records sorted a the hash key reference
        if (cursor.record.getKey() != k) { //if the key at cursor.record does not equal key (k), then move onto the cursor.next
            return cursor.next.record;
        }
    }
    return null;
}
记录类

public class Record<T, U> {

private T key;//Contacts name, and the value that is ultimately hashed. It is then inserted, searched and deleted
private U data;//This data is the Contacts address, when the key is hashed, nothing is done to this value except that it is 
//either stored or retrieved from the hash table when the key is used

public T getKey() {
    return key;//returns the value stored as a key
}

public void setKey(T k) {
    this.key = k;//used during the insert operation to set key's value.
}

public U getData(T k) {//retrieve the data that is stored with an associated key that has been updated, searhed or is being written to a file
    return data;
}

public void setData(U data) {//adds the data to the records data element
    this.data = data;
}

public int hashCode(T k) {//When this hash code function is called, it returns a mathematical representation of the key, that was passed to it
    //it returns the absolute value of the generic hashCode() function. Further computations are required in the Table class, since the hash created here
    //can be very large and would throw and exception.  For example, the hash for "Chris" after this computation has been performed is 94639767, which is
    //much larger than our array. So this will cause an IndexOutOfBoundsException().
    return Math.abs(k.hashCode());
}

public boolean equals(Record<T, U> r) {
    //this equals method, doesn't override the generic equals() method provided by Java. Instead, this method is created to use instead of the generic
    //equals method. When this is called, the has value computed above, with the additional math from the Table class, is compared to all of the elements
    //in the array. If a match is found, this returns true
    return key.equals(r.key);
}
}
公共类记录{
private T key;//联系人名称和最终哈希的值。然后插入、搜索和删除该值
private U data;//此数据是联系人地址,对密钥进行哈希运算时,不会对此值执行任何操作,除非它是
//使用密钥时存储或从哈希表中检索
公共T getKey(){
return key;//返回作为键存储的值
}
公共无效设置密钥(TK){
this.key=k;//在插入操作期间用于设置键的值。
}
public U getData(T k){//检索与已更新、搜索或正在写入文件的关联密钥一起存储的数据
返回数据;
}
public void setData(U data){//将数据添加到records数据元素
这个数据=数据;
}
public int hashCode(tk){//调用此哈希码函数时,它返回传递给它的键的数学表示形式
//它返回泛型hashCode()函数的绝对值。由于在此处创建了哈希,因此需要在Table类中进行进一步计算
//可以非常大,并且会抛出和异常。例如,执行此计算后“Chris”的哈希为94639767,这是
//比数组大得多。因此这将导致IndexOutOfBoundsException()。
返回Math.abs(k.hashCode());
}
公共布尔等于(记录r){
//此equals方法不会覆盖Java提供的泛型equals()方法。相反,创建此方法是为了使用它而不是泛型equals()方法
//当调用此方法时,上面计算的has值与表类中的附加数学值将与所有元素进行比较
//如果找到匹配项,则返回true
返回键。等于(r键);
}
}

这是一个经典的==vs.equals()问题

a[i].record.getKey()!=k
可以为true,而a[i].record.getKey().equals(k)也可以为true


您应该使用
(!a[i].record.getKey().equals(k))
而不是
a[i].record.getKey()!=这是一个经典的==vs.equals()问题

a[i].record.getKey()!=k
可以为true,而a[i].record.getKey().equals(k)
也可以为true


您应该使用
(!a[i].record.getKey().equals(k))
而不是
a[i].record.getKey()!=k

非常感谢您!我以为我今天试过了,但显然没有。我得等几分钟才能把它标记为答案……非常感谢!我以为我今天试过了,但显然没有。我必须等待几分钟才能将其标记为答案…我希望您将此作为练习,而不是在实际应用程序中使用。如果是后者,你应该强烈地考虑使用库HasMead类。我只是想了解java提供给我们的背景……我希望你这样做是一个练习而不是在实际应用中使用。如果是后者,你应该强烈地考虑使用库HasMead类,我只是想了解java提供给我们的背景…