Question: Implement MyMap using Open Addressing with linear probing. (exercise 27.1 in Liang Java textbook, p. 1012). Create a new concrete class that implements MyMap using
Implement MyMap using Open Addressing with linear probing. (exercise 27.1 in Liang Java textbook, p. 1012). Create a new concrete class that implements MyMap using open addressing with linear probing. For simplicity, use f(key) = key % sizeas the hash function, where size is the hash-table size. Initially, the hash table size is 4. The table size is doubled the load factor exceeds the threshold of 0.5
A zipped up version of the MyHashMap NetBeans project (from Ch27 of the Liang Java textbook) is attached
---------------
package myhashmap;
import java.util.LinkedList;
public class MyHashMap
// Specify a load factor used in the hash table private float loadFactorThreshold; // The number of entries in the map private int size = 0; // Hash table is an array with each cell that is a linked list LinkedList
/** Construct a map with the default capacity and load factor */ public MyHashMap() { this(DEFAULT_INITIAL_CAPACITY, DEFAULT_MAX_LOAD_FACTOR); } /** Construct a map with the specified initial capacity and * default load factor */ public MyHashMap(int initialCapacity) { this(initialCapacity, DEFAULT_MAX_LOAD_FACTOR); } /** Construct a map with the specified initial capacity * and load factor */ public MyHashMap(int initialCapacity, float loadFactorThreshold) { if (initialCapacity > MAXIMUM_CAPACITY) this.capacity = MAXIMUM_CAPACITY; else this.capacity = trimToPowerOf2(initialCapacity); this.loadFactorThreshold = loadFactorThreshold; table = new LinkedList[capacity]; } @Override /** Remove all of the entries from this map */ public void clear() { size = 0; removeEntries(); }
@Override /** Return true if the specified key is in the map */ public boolean containsKey(K key) { if (get(key) != null) return true; else return false; } @Override /** Return true if this map contains the value */ public boolean containsValue(V value) { for (int i = 0; i < capacity; i++) { if (table[i] != null) { LinkedList
@Override /** Return the value that matches the specified key */ public V get(K key) { int bucketIndex = hash(key.hashCode()); if (table[bucketIndex] != null) { LinkedList
// Add a new entry (key, value) to hashTable[index] table[bucketIndex].add(new MyMap.Entry
size++; // Increase size return value; } @Override /** Remove the entries for the specified key */ public void remove(K key) { int bucketIndex = hash(key.hashCode()); // Remove the first entry that matches the key from a bucket if (table[bucketIndex] != null) { LinkedList
/** Return a power of 2 for initialCapacity */ private int trimToPowerOf2(int initialCapacity) { int capacity = 1; while (capacity < initialCapacity) { capacity <<= 1; } return capacity; } /** Remove all entries from each bucket */ private void removeEntries() { for (int i = 0; i < capacity; i++) { if (table[i] != null) { table[i].clear(); } } } /** Rehash the map */ private void rehash() { java.util.Set
@Override public String toString() { StringBuilder builder = new StringBuilder("["); for (int i = 0; i < capacity; i++) { if (table[i] != null && table[i].size() > 0) for (Entry
___________
package myhashmap;
/** * * @author wrcronin */ public interface MyMap
/** Return a set of entries in the map */ public java.util.Set
/** 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
/** 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
------------------------------
package myhashmap;
public class TestMyHashMap { public static void main(String[] args) { // Create a map MyMap
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"); System.out.println("Entries in map: " + map);
map.clear(); System.out.println("Entries in map: " + map); } }
Thank you
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
