![](/img/trans.png)
[英]Hash function for creating a generic hash table in Java (for learning purposes)
[英]Java Hash Table Search Function
我創建了一個哈希表,但似乎陷入了一個問題。 我在哈希表中有數據,當搜索數據時,它按預期返回。 但是,如果我搜索的內容不在表中,但仍散列到存在的元素中,則它不會返回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);
}
}
這是一個經典的== vs .equals()問題。
a[i].record.getKey() != k
可以為真,而a[i].record.getKey().equals(k)
也可以為真。
您應該使用(!a[i].record.getKey().equals(k))
而不是a[i].record.getKey() != k
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.