Question: Java The method enlargeHashTable does not retain the instances of Entry that were in the old hash table. It could if the method add had

Java
The method enlargeHashTable does not retain the instances of Entry that were in the old hash table. It could if the method add had an entry as its parameter instead of the search key and value. Write such a method as an additional but private add method, and then revise enlargeHashTable so it retains the instances of Entry that were in the old hash table.
// Old add method:
public V add(K key, V value){
if ((key == null)||(value == null)){
throw new NoSuchElementException("Key or value not found.");
}
int index = getHashIndex(key);
V oldValue;
if (!contains(key)){
hashTable[index]= new Entry(key, value);
numberOfEntries++;
oldValue = null;
} else {
oldValue = hashTable[index].getValue();
hashTable[index].setValue(value);
}
if (isHashTableTooFull()){
enlargeHashTable();
}
return oldValue;
}
private void enlargeHashTable(){
Entry[] oldTable = hashTable;
int oldSize = hashTable.length;
int newSize = getNextPrime(oldSize + oldSize);
checkSize(newSize);
Entry[] temp =(Entry[]) new Entry[newSize];
hashTable = temp;
numberOfEntries =0;
for (int index =0; index < oldSize; index++){
if ((oldTable[index]!= null) && (oldTable[index]!= AVAILABLE)){
add(oldTable[index].getKey(), oldTable[index].getValue());
}// end for
}// end enlargeHashTable
}

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 Programming Questions!