Question: how would i store the hash table created from this java code into a random access file and do all operations from that file? class

how would i store the hash table created from this java code into a random access file and do all operations from that file?

class MapEntry

{

K key;

V value;

// Reference to next node

MapEntry next;

// Constructor

public MapEntry(K key, V value)

{

this.key = key;

this.value = value;

}

}

//Class to represent entire hash table

public class HashTable

{

// bucketArray is used to store array of chains

private ArrayList> bucketArray;

// Current capacity of array list

private int numBuckets;

// Current size of array list

private int size;

// Constructor (Initializes capacity, size and

// empty chains.

public HashTable(int capacity)

{

bucketArray = new ArrayList<>(capacity);

numBuckets = capacity;

size = 0;

// Create empty chains

for (int i = 0; i < numBuckets; i++)

bucketArray.add(null);

}

public int size() { return size; }

public boolean isEmpty() { return size() == 0; }

// This implements hash function to find index

// for a key

private int getBucketIndex(K key)

{

int hashCode = key.hashCode();

int index = hashCode % numBuckets;

return index;

}

// Method to remove a given key

public V remove(K key)

{

// Apply hash function to find index for given key

int bucketIndex = getBucketIndex(key);

// Get head of chain

MapEntry head = bucketArray.get(bucketIndex);

// Search for key in its chain

MapEntry prev = null;

while (head != null)

{

// If Key found

if (head.key.equals(key))

break;

// Else keep moving in chain

prev = head;

head = head.next;

}

// If key was not there

if (head == null)

return null;

// Reduce size

size--;

// Remove key

if (prev != null)

prev.next = head.next;

else

bucketArray.set(bucketIndex, head.next);

return head.value;

}

// Returns value for a key

public V get(K key)

{

// Find head of chain for given key

int bucketIndex = getBucketIndex(key);

MapEntry head = bucketArray.get(bucketIndex);

// Search key in chain

while (head != null)

{

if (head.key.equals(key))

return head.value;

head = head.next;

}

// If key not found

return null;

}

// Adds a key value pair to hash

public void add(K key, V value)

{

// Find head of chain for given key

int bucketIndex = getBucketIndex(key);

MapEntry head = bucketArray.get(bucketIndex);

// Check if key is already present

while (head != null)

{

if (head.key.equals(key))

{

head.value = value;

return;

}

head = head.next;

}

// Insert key in chain

size++;

head = bucketArray.get(bucketIndex);

MapEntry newNode = new MapEntry(key, value);

newNode.next = head;

bucketArray.set(bucketIndex, newNode);

// If load factor goes beyond threshold, then

// double hash table size

if ((1.0*size)/numBuckets >= 0.7)

{

ArrayList> temp = bucketArray;

bucketArray = new ArrayList<>();

numBuckets = 2 * numBuckets;

size = 0;

for (int i = 0; i < numBuckets; i++)

bucketArray.add(null);

for (MapEntry headNode : temp)

{

while (headNode != null)

{

add(headNode.key, headNode.value);

headNode = headNode.next;

}

}

}

}

// Driver method to test Map class

public static void main(String[] args)

{

HashTablemap = new HashTable<>(16);

map.add("this",1 );

map.add("coder",2 );

map.add("this",4 );

map.add("hi",5 );

System.out.println(map.size());

System.out.println(map.remove("this"));

System.out.println(map.remove("this"));

System.out.println(map.size());

System.out.println(map.isEmpty());

}

}

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!