Question
Make a class called MyHashMap which implements a map using a hash table. The MyHashMap class must have the methods listed in image below: The
Make a class called MyHashMap which implements a map using a hash table. The MyHashMap class must have the methods listed in image below: The only special method is printTable which should output how many conflicts occur at each array slot (or bucket) and list the keys at that slot. The hashTable should always have 8 slots. An example output for printTable is shown in the documentation. It is recommended to use an ArrayList of linked lists. The linked lists can be the Java LinkedList class or your own linked list implementation where the key,value pairs link to each other, whatever one prefer. Here is the code for the hash function. One should use this exact code in your project: private int hash(K key) { int hashCode = key.hashCode(); int index = hashCode % numBuckets; return Math.abs(index); } This hash map will use chaining to handle collisions. The hash map should only have 8 slots. Collisions will occur. Put the (key,value) pairs at the head of the linked list. If the same key is passed in with a different value, do the value update 'in place'.
public void printTable() Outputs how many conflicts occur at each bucket and list the keys in that bucket. Example output for this method:
Index 0: (0 conflicts), [] Index 1: (0 conflicts), [] Index 2: (0 conflicts), [] Index 3: (0 conflicts), [] Index 4: (0 conflicts), [] Index 5: (0 conflicts), [ExampleKeyX, ] Index 6: (0 conflicts), [ExampleKeyY, ] Index 7: (0 conflicts), [] Total # of conflicts: 0
void clear ( ) Removes all of the mappings from this map. boolean containsKey (K key) Returns true if this map contains a mapping for the specified key. boolean containsValue Returns true if this map maps one or (V val) more keys to the specified value. V get (K key) Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. boolean isEmpty ( ) Returns true if this map contains no key- value mappings. java. util. SetStep by Step Solution
There are 3 Steps involved in it
Step: 1
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