Question: JAVA: Write a remove, rehash and size methods for HashTableChain Class: import java.util.Iterator; import java.util.LinkedList; import java.util.Map; public class HashtableChain implements KWHashMap { private LinkedList
JAVA: Write a remove, rehash and size methods for HashTableChain Class:
import java.util.Iterator; import java.util.LinkedList; import java.util.Map;
public class HashtableChain
private static class Entry
public Entry(K key, V value) { this.key = key; this.value = value; }
@Override public K getKey(){ return key; }
@Override public V getValue() { return value; }
@Override public V setValue(V val) { V oldVal = value; value = val; return oldVal; }
}
public HashtableChain() { table = new LinkedList[CAPACITY]; } @Override public V get(Object key) { int index = key.hashCode() % table.length; if (index < 0) { index += table.length; } if (table[index] == null) { return null; // key is not in the table. } // Search the list at table[index] to find the key. for (Entry
// assert: key is not in the table. return null; }
@Override public V put(K key, V value) { int index = key.hashCode() % table.length; if (index < 0) { index += table.length; } if (table[index] == null) { // Create a new linked list at table[index]. table[index] = new LinkedList
// Search the list at table[index] to find the key. for (Entry
// assert: key is not in the table, add new item. table[index].addFirst(new Entry
public boolean isEmpty() { return numKeys == 0; } public int size(){ return table.length; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
