Question: Your task is to go through and implement the methods getBucketIndex, add, get, and remove. Follow the comments inside respective methods. import java.util.ArrayList; import java.util.Scanner;

Your task is to go through and implement the methods getBucketIndex, add, get, and remove. Follow the comments inside respective methods.
import java.util.ArrayList;
import java.util.Scanner;
public class HashtableChaining {
// Hashtable bucket
private ArrayList> bucket;
// Current capacity of the array list
private int numBuckets;
// current size of the array list
private int size;
public HashtableChaining(int buckets){
bucket = new ArrayList<>();
numBuckets = buckets;
size =0;
// create empty chains
for(int i =0; i < numBuckets; i++){
bucket.add(null);
}
}
public int size(){
return size;
}
public boolean isEmpty(){
return size()==0;
}
private int getBucketIndex(K key){
// Implement Multiplicative hash
// Assume INITIAL_VALUE =7 HASH_MULTIPLIER =3
}
//Adds a key value pair to hash
public void add(K key, V value){
// Find head of chain for given key
// Check if key is already present
// Insert key in chain
// If load factor goes beyond threshold (0.75), then double the hash size
// and rehash the existing items
// print "Doubling the size, load factor >0.75" when you double the hash size.
}
// Returns value for a key
public V get(K key){
// Find head of chain for given key
// Search key in chain
// If key not found return null
return null;
}
// Removes key from the hashtable
public V remove(K key){
// Apply hash function to find index for a given key
// get head of the chain
// Search for key in its chain
// If key is found
// Reduce size
// Remove key and return
// Else keep moving in the chain
//If key was not there return null
return null;
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the initial capacity:");
int size = input.nextInt();
HashtableChaining hashtable = new HashtableChaining<>(size);
System.out.println("Adding items to hashtable:");
hashtable.add("FALL2017",3);
hashtable.add("SPRING2018",3);
hashtable.add("SUMMER2018",1);
hashtable.add("FALL2018",4);
hashtable.add("SPRING2019",2);
hashtable.add("SPRING2019",3);
System.out.printf("Size of the hashtable is %d%n", hashtable.size());
System.out.println("Removing key SUMMER2018");
System.out.printf("The value is %d%n", hashtable.remove("SUMMER2018"));
System.out.printf("Now the size of the hashtable is %d%n",
hashtable.size());
System.out.println("Looking up key SUMMER2018");
System.out.printf("The value is %s%n",
hashtable.get("SUMMER2018"));
System.out.printf("Is the hashtable empty? :%s%n",
hashtable.isEmpty()? "True" : "False");
input.close();
}
}

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!