Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management With Website Development Applications

Authors: Greg Riccardi

1st Edition

0201743876, 978-0201743876

More Books

Students also viewed these Databases questions

Question

=+1. What is discipline?

Answered: 1 week ago

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago