Question: Can you implement MyMap using open addressing with linear probing. Create a new concrete class named MyHashMap that implements MyMap using open addressing with linear

Can you implement MyMap using open addressing with linear probing. Create a new concrete class named MyHashMap that implements MyMap using open addressing with linear probing. For simplicity, use f(key)= key % size as the hash function, where size is the hash-table size. Initially, the hash-table size is 4. The table size is doubled whenever the load factor exceeds the threshold (0.5).
Modify TestMyHashMap.java slightly and use it to test MyHashMap. Your output should be as follows:
Entries in map: [[Lewis,29][Cook,29][Anderson,31][Smith,65]]
The age for Lewis is 29
Is Smith in the map? true
Is age 33 in the map? false
Entries in map after removing Smith: [[Lewis,29][Cook,29][Anderson,31]]
Entries after clearing map: []
----------------------------------------------------------------------------------------------
TestMyHashMap class:
public class TestMyHashMap {
public static void main(String[] args){
// Create a map
MyMap map = new MyHashMap<>();
map.put("Smith",30);
map.put("Anderson",31);
map.put("Lewis",29);
map.put("Cook",29);
map.put("Smith",65); // Add Smith with age 65 to map
System.out.println("Entries in map: "+ map);
System.out.println("The age for "+ "Lewis is "+
map.get("Lewis"));
System.out.println("Is Smith in the map? "+
map.containsKey("Smith"));
System.out.println("Is age 33 in the map? "+
map.containsValue(33));
map.remove("Smith"); // Remove Smith from map
System.out.println("Entries in map: "+ map);
map.clear();
System.out.println("Entries in map: "+ map);
}
}
------------------------------------------------------------------
MyMap class:
public interface MyMap {
/** Remove all of the entries from this map */
public void clear();
/** Return true if the specified key is in the map */
public boolean containsKey(K key);
/** Return true if this map contains the specified value */
public boolean containsValue(V value);
/** Return a set of entries in the map */
public java.util.Set> entrySet();
/** Return the first value that matches the specified key */
public V get(K key);
/** Return true if this map contains no entries */
public boolean isEmpty();
/** Return a set consisting of the keys in this map */
public java.util.Set keySet();
/** Add an entry (key, value) into the map */
public V put(K key, V value);
/** Remove the entries for the specified key */
public void remove(K key);
/** Return the number of mappings in this map */
public int size();
/** Return a set consisting of the values in this map */
public java.util.Set values();
/** Define inner class for Entry */
public static class Entry {
K key;
V value;
public Entry(K key, V value){
this.key = key;
this.value = value;
}
public K getKey(){
return key;
}
public V getValue(){
return value;
}
@Override
public String toString(){
return "["+ key +","+ value +"]";
}
}
}

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 Programming Questions!