I've been assigned the following task in my Datastructures and Algorithms course, and I have no idea why it does not work.
The task is to implement a Linked Hashtable. I have created two classes, one that acts as the key <-> value pair, representing an element (Entry class), and the Hashtable class that has the methods (currently only put and get), but I can't seem any of them to work. The Hashfunction method was provided by our teachers, so I can't answer any questions regarding that.
Whenever I execute the program, I get no errors, but the list returns empty. Anyone here who can guide towards the right direction of where I am doing wrong? I assume the error lies within the put method, but I can't seem to figure out where to issue might be.
Best regards, Victor
package Laboration2;
/**
* A class that works as a container for the key and value.
* 'Entry' will become an element in our hashtable
*
* @author Victor Marante
* @version 1.0
* @since 2016-09-22
*/
public class Entry {
private Object key;
private Object value;
private Entry next;
public Entry(Object key, Object value) {
this.key = key;
this.value = value;
}
@Override
public boolean equals(Object obj) {
Entry keyToCompare = new Entry(obj, null);
return key.equals(keyToCompare.key);
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Object getKey() {
return key;
}
public Entry getNext() {
return next;
}
public void setNext(Entry next) {
this.next = next;
}
}
Class that holds all the methods for the Hashtable itself:
package Laboration2;
import javax.swing.*;
import java.util.Iterator;
import java.util.LinkedList;
/**
* Created by Victor on 22/09/16.
*/
public class Hashtable {
private LinkedList<Object> insertionOrder = new LinkedList<Object>();
private LinkedList<Entry>[] table;
// Constructor that initiates a hashtable
public Hashtable(int size) {
table = (LinkedList<Entry>[]) new LinkedList<?>[size];
for (int i = 0; i < size; i++) {
table[i] = new LinkedList<Entry>();
}
}
// Hashfunction
private int hashIndex(Object key) {
int hashCode = key.hashCode();
hashCode = hashCode % table.length;
return (hashCode < 0) ? -hashCode : hashCode;
}
public Object get(Object key) {
int hashIndex = hashIndex(key);
LinkedList<Entry> entries = table[hashIndex];
Iterator<Entry> it = entries.listIterator();
while (it.hasNext()) {
Entry entry = it.next();
if (entry.equals(key)) {
return entry.getValue();
}
}
return null;
}
public void put(Object key, Object value) {
int hashIndex = hashIndex(key);
LinkedList<Entry> entries = table[hashIndex];
Iterator<Entry> it = entries.listIterator();
while (it.hasNext()) {
Entry entry = it.next();
if (entry.equals(key)) {
entry.setValue(value);
insertionOrder.add(value);
} else {
entry.setNext(new Entry(key, value));
insertionOrder.add(value);
}
}
}
public static void main(String[] args) {
Hashtable table = new Hashtable(15);
table.put("hej", "hello");
table.put("nej", "no");
table.put("senare", "later");
table.put("idag", "today");
table.put("igår", "yesterday");
table.get("hej");
}
}
EDIT1 (for Krishas comment):
public void put(Object key, Object value) {
int hashIndex = hashIndex(key);
LinkedList<Entry> entries = table[hashIndex];
Iterator<Entry> it = entries.listIterator();
if (table[hashIndex] == null) {
table[hashIndex] = new LinkedList<Entry>(key, value);
} else {
while (it.hasNext()) {
Entry entry = it.next();
if (entry.equals(key)) {
entry.setValue(value);
insertionOrder.add(value);
} else {
entry.setNext(new Entry(key, value));
insertionOrder.add(value);
}
}
}
}