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 implements KWHashMap { private LinkedList>[] table; private int numKeys; private static final int CAPACITY = 101; private static final double LOAD_THRESHOLD = 3.0;

private static class Entry implements Map.Entry { private K key; private V value;

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 nextItem : table[index]) { if (nextItem.key.equals(key)) { return nextItem.value; } }

// 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 nextItem : table[index]) { // If the search is successful, replace the old value. if (nextItem.key.equals(key)) { // Replace value for this key. V oldVal = nextItem.value; nextItem.setValue(value); return oldVal; } }

// assert: key is not in the table, add new item. table[index].addFirst(new Entry(key, value)); numKeys++; if (numKeys > (LOAD_THRESHOLD * table.length)) { // rehash(); } return null; }

public boolean isEmpty() { return numKeys == 0; } public int size(){ return table.length; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!