Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed
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. Set keySet ( ) Returns a Set view of the keys contained in this map. void printTable( ) Outputs how many conflicts occur at each bucket and list the keys in that bucket. V put (K key, V val) Associates the specified value with the specified key in this map. V remove (K key) Removes the mapping for the specified key from this map if present. int size( ) Returns the number of key-value mappings in this map

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

Step: 3

blur-text-image

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

Pollution Human Activities?

Answered: 1 week ago

Question

Major global environmental Threats ?

Answered: 1 week ago

Question

Socratic method ?

Answered: 1 week ago