Question
Problem. Build a hash table for storing strings from scratch. Use chaining strategy for handling collisions. Implement a HashTable class with the following methods: -
Problem. Build a hash table for storing strings from scratch. Use chaining strategy for handling collisions. Implement a HashTable class with the following methods:
- put(int key, String val) that adds an entry with the key key and the value val; - get(int key) that returns the value (of type String) with the key equal to key; - remove(int key) that removes the entry with the key equal to key. Estimate runtime complexities of the methods that you built.
- To build the HashTable class, you need to implement a small private class Entry inside the HashTable. You may do it, for instance, like that: private class Entry { private int key; private String val; public Entry(int key, String val) { this.key = key; this.val = val; } } Then you can declare your HashTable class in the following way: public class HashTable { private LinkedList[] table; 1public HashTable(int capacity) { table = new LinkedList[capacity]; } } - To test your data structure, you can either use a debugger (a preferred method), or write your own toString method1, for example like this: public String toString() { StringBuilder str = new StringBuilder(); for (int i = 0; i < table.length; i++) { LinkedList bucket = table[i]; String delimiter = ""; if (bucket == null) { str.append("[] "); continue; } str.append("["); for (var entry : bucket) { str.append(delimiter + entry.key
+ "=" + entry.val); delimiter = ", "; } str.append("] "); } return str.toString(); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Sure Heres an implementation of the HashTable class with the put get and remove methods using chaining for handling collisions java import javautilLin...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started