I have this implementation for inserting into a hashtable that uses sequential chaining:
public void insert(String word, Definition definition) {
int hash = hashFunction(word);
if (table[hash] == null) {
EntryImplSub chainedEntry = new EntryImplSub(null);
chainedEntry.addDefinition(definition);
chainedEntry.setWord(word);
table[hash] = chainedEntry;
numProbes++;
entries++;
}
else{
EntryImplSub chainedEntry = new EntryImplSub(table[hash]);
chainedEntry.addDefinition(definition);
chainedEntry.setWord(word);
table[hash] = chainedEntry;
numProbes++;
}
}
Essentially, I am trying to make a dictionary. There is an EntryImpl class that acts as an entry object, and each entry has a word and definition (or multiple definitions). Now I have made a new extended class EntryImplSub that is meant to be chained. This new class has a getNext() method and inherits all the other functionality from the normal EntryImpl class. The EntryImplSub constructor is used to set next.
Question
However this is not working. When I load in a large number of words they are not all entered. And I am not sure why.
My Attempt
My logic with this implementation is that if the table entry is null, we insert a new EntryImplSub object with next = null, and then set the word and definition.
If however there is already a word at the position we are trying to insert into, then we must add our new entry to the front of the list. So we create a new EntryImplSub object with the next attribute being set to whatever was already in the table. Then I set the word for the new EntryImplSub and insert this into the table. So we should have a linked list of EntryImplSub.
I am really not sure why this inst working. I've spent hours trying to find an error, and any help would be appreciated. Let me know if you need clarification on anything.
Thanks a lot!
EDIT
Here is the code that checks if an entry is in the table
public List<Definition> getDefinitions(String word) {
int hash = hashFunction(word);
//ChainedEntry head = table[hash];
while (table[hash] != null) {
numSearches++;
if (((EntryImpl) table[hash]).getWord().equals(word)) {
return ((EntryImpl) table[hash]).getDefinitions();
}
table[hash] = table[hash].getNext();
}
return null;
}
If this returns null, then the word is not in the table