Java 哈希表中的搜索函数
我不确定是否应该像这样创建Java 哈希表中的搜索函数,java,algorithm,data-structures,linked-list,hashtable,Java,Algorithm,Data Structures,Linked List,Hashtable,我不确定是否应该像这样创建Node[]size public class HashTable { /* Class containing next node and key and name value*/ class Node { private long key; public long getKey() { return key; } public void setKey(lo
Node[]size
public class HashTable {
/* Class containing next node and key and name value*/
class Node {
private long key;
public long getKey() {
return key;
}
public void setKey(long key) {
this.key = key;
}
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
Node next;
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
Node(long key, String value) {
this.key = key;
this.value = value;
next = null;
}
}
我的hashFunction(long key)可以工作,但是我在实现搜索函数的逻辑时遇到了问题。我不确定我做错了什么。有人能帮忙吗。
我的搜索实现从
公共节点搜索(长键)
开始,您使用的是节点列表=大小[hash]
;在搜索中,但正如我在参数构造函数中看到的,您正在使用array=newnode[size]代码>来初始化它
因此,尝试用数组[hash]
替换大小[hash]
在粘贴的代码中,我没有看到任何使用Node[]size
声明的情况。您可以删除size数组变量。我认为在给定代码中没有。您是否管理了碰撞场景?如果您使用hash方法为两个不同的值返回相同的hashcode会怎么样。你是如何加入它并从中获得的?我只是不能说实现是正确的。对于初始的不同元素,它会起作用。我需要完整的实现来进一步检查
Node[] size;
//The hash table array
Node[] array = null;
//hash table size. We typically choose m to be a power of 2
int m = 0;
// Constructor
HashTable(int size) {
m = size;
array = new Node[size];
}
HashTable() {
this(100);
}
// Search for a given key in the hashtable
// Note: you need to use hashFunction(long key) to determine the slot (array index) you will search in
public Node search(long key)
{
Node node = null;
/**
* TODO
*/
int hash = hashFunction(key);
Node list = size[hash];
while(list!=null) {
if(list.getKey() == key) {
return list;
}
else {
list = list.getNext();
}
}
return node;
}