Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are given the file HashMap.java. Modify this implementation of a HashMap so that it becomes MediaHashMap.java. This new class will be a HashMap that

You are given the file HashMap.java. Modify this implementation of a HashMap so that it becomes MediaHashMap.java. This new class will be a HashMap that uses a String id as key and Media objects (attached file) as values . Make sure your indentation matches coding standards and you add a class header for full credit.

Once MediaHashMap is complete, write a separate class to test your MediaHashMap implementation. Add at least three instances of Media to the HashMap. Then, remove at least one and print the remaining instances from the HashMap, dont just print them as you add them. I want to see you print the contents of the HashMap.

import java.util.LinkedList;

public class HashMap implements MyMap { // Define the default hash table size. Must be a power of 2 private static int DEFAULT_INITIAL_CAPACITY = 4; // Define the maximum hash table size. 1 << 30 is same as 2^30 private static int MAXIMUM_CAPACITY = 1 << 30; // Current hash table capacity. Capacity is a power of 2 private int capacity; // Define default load factor private static float DEFAULT_MAX_LOAD_FACTOR = 0.75f;

// 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>[] table;

/** Construct a map with the default capacity and load factor */ public HashMap() { this(DEFAULT_INITIAL_CAPACITY, DEFAULT_MAX_LOAD_FACTOR); } /** Construct a map with the specified initial capacity and * default load factor */ public HashMap(int initialCapacity) { this(initialCapacity, DEFAULT_MAX_LOAD_FACTOR); } /** Construct a map with the specified initial capacity * and load factor */ public HashMap(int initialCapacity, float loadFactorThreshold) { if (initialCapacity > MAXIMUM_CAPACITY) this.capacity = MAXIMUM_CAPACITY; else this.capacity = trimToPowerOf2(initialCapacity); this.loadFactorThreshold = loadFactorThreshold; table = new LinkedList[capacity]; } /** Remove all of the entries from this map */ public void clear() { size = 0; removeEntries(); }

/** Return true if the specified key is in the map */ public boolean containsKey(K key) { return get(key) != null; } /** 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> bucket = table[i]; for (Entry entry: bucket) if (entry.getValue().equals(value)) return true; } } return false; } /** Return a set of entries in the map */ public java.util.Set> entrySet() { java.util.Set> set = new java.util.HashSet>(); for (int i = 0; i < capacity; i++) { if (table[i] != null) { LinkedList> bucket = table[i]; for (Entry entry: bucket) set.add(entry); } } return set; }

/** Return the value that matches the specified key */ public V get(K key) { int bucketIndex = hash(key.hashCode()); if (table[bucketIndex] != null) { LinkedList> bucket = table[bucketIndex]; for (Entry entry: bucket) if (entry.getKey().equals(key)) return entry.getValue(); } return null; } /** Return true if this map contains no entries */ public boolean isEmpty() { return size == 0; } /** Return a set consisting of the keys in this map */ public java.util.Set keySet() { java.util.Set set = new java.util.HashSet(); for (int i = 0; i < capacity; i++) { if (table[i] != null) { LinkedList> bucket = table[i]; for (Entry entry: bucket) set.add(entry.getKey()); } } return set; } /** Add an entry (key, value) into the map */ public V put(K key, V value) { if (get(key) != null) { // The key is already in the map int bucketIndex = hash(key.hashCode()); LinkedList> bucket = table[bucketIndex]; for (Entry entry: bucket) if (entry.getKey().equals(key)) { V oldValue = entry.getValue(); // Replace old value with new value entry.value = value; // Return the old value for the key return oldValue; } } // Check load factor if (size >= capacity * loadFactorThreshold) { if (capacity == MAXIMUM_CAPACITY) throw new RuntimeException("Exceeding maximum capacity"); rehash(); } int bucketIndex = hash(key.hashCode()); // Create a linked list for the bucket if it is not created if (table[bucketIndex] == null) { table[bucketIndex] = new LinkedList>(); }

// Add a new entry (key, value) to hashTable[index] table[bucketIndex].add(new MyMap.Entry(key, value));

size++; // Increase size return value; } /** 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> bucket = table[bucketIndex]; for (Entry entry: bucket) if (entry.getKey().equals(key)) { bucket.remove(entry); size--; // Decrease size break; // Remove just one entry that matches the key } } } /** Return the number of entries in this map */ public int size() { return size; } /** Return a set consisting of the values in this map */ public java.util.Set values() { java.util.Set set = new java.util.HashSet(); for (int i = 0; i < capacity; i++) { if (table[i] != null) { LinkedList> bucket = table[i]; for (Entry entry: bucket) set.add(entry.getValue()); } } return set; } /** Hash function */ private int hash(int hashCode) { return supplementalHash(hashCode) & (capacity - 1); } /** Ensure the hashing is evenly distributed */ private static int supplementalHash(int h) { h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); }

/** 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> set = entrySet(); // Get entries capacity <<= 1; // Double capacity table = new LinkedList[capacity]; // Create a new hash table size = 0; // Reset size to 0 for (Entry entry: set) { put(entry.getKey(), entry.getValue()); // Store to new table } }

public String toString() { StringBuilder builder = new StringBuilder("["); for (int i = 0; i < capacity; i++) { if (table[i] != null && table[i].size() > 0) for (Entry entry: table[i]) builder.append(entry); } builder.append("]"); return builder.toString(); } }

----------------------------------------------------------

/** * Class: Media * * @author Dr. Johnson * @version 1.0 Course : ITEC 3150, Fall, 2015 Written: January 18, 2012 * * * This class ? This class describes the common attributes and methods * of the media being collected into a library * * Purpose: ? This class is intended to serve a parent class only for * all different types of media * */ public class Media { private String idNumber; private String itemName; private String type;

/** * Method:Media () * * Constructor method that accepts values for all the attributes and sets * them. * * @param idNumber * @param itemName * @param type */ public Media(String idNumber, String itemName, String type) { this.idNumber = idNumber; this.itemName = itemName; this.type = type; }

/** * Method:getIdNumber() Getter method for the idNumber attribute * * @return the idNumber */ public String getIdNumber() { return idNumber; }

/** * Method:getItemName() Getter method for the itemName attribute * * @return the itemName */ public String getItemName() { return itemName; }

/* * Method:toString() Converts the attributes of the class to a text readable * format. * * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "Media [idNumber=" + idNumber + ", itemName=" + itemName + ", type=" + type + "]"; }

/** * Method:getItemName() Getter method for the itemName attribute * * @return the type */ public String getType() { return type; }

}

------------------------------------------------

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

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

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions