Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How is HashTable.java done? HashTable Implement the map interface methods in the HashTable class. Each element in the hash table should be maintained by an

How is HashTable.java done?

HashTable

Implement the map interface methods in the HashTable class. Each element in the hash table should be maintained by an instance of a ListNode which provides fields for the key-value pair associated with a hash table element and a reference for any potential next element that may follow a given hash table element in one of the lists. The constructor for the class takes the number of buckets in the linked-list as a parameter. Ratio of the number of buckets in the linked-list to the number of elements stored in the entire hash table is a key heavily influences the efficiency of map operations in the hash table. Recall that a hash table is an array of linked-lists and the hash function is used to compute the index of the linked-list in the array where a key must be stored. When searching or inserting, hash the key using the hash function to compute the index of the bucket in which the key-value pair should be stored. Also recall that the key must be unique which means that upon insert and once the bucket is identified through hashing, perform a search on the key in the associated linked-list to determine whether the key is already stored in the linked-list before performing any insertion. This means it cannot arbitrarily perform tail insertions on the linked-list, so there is no benefit of storing a tail pointer because it will end up searching for the tail anyway. For the sake of profiling comparisons, only count the comparisons when searching the linked-list itself.

/* TODO - Summarize this class */ public class HashTable implements Map {    // TODO - Document all instance variables   private final ListNode[] buckets;    public HashTable(int length) {     this.buckets = new ListNode[length];   }    /// TODO - Document this method   public void insert(String key, String value, int[] profile) {     // TODO: write this method   }    /// TODO - Document this method   public String search(String key, int[] profile) {     // TODO: write this method     return null;   }    /// TODO - Document this method   public void clear() {     // TODO: write this method    }    /// The number of buckets in the hash table   /// @return the number of buckets in the hash table   public int length() {     return buckets.length;   }   /// Hashing function converts a string of characters into an index   /// bounded by the size of the hash table's array   /// @param key input to hash   /// @return bucket index corresponding to the hashed key   private int hash(String key) {     return Math.abs(key.hashCode() % buckets.length);   } } 

Other useful files to go with it:

public interface Map {   public void insert(String key, String value, int[] profile);   public String search(String key, int[] profile);   public void clear(); } 

public class ListNode {    private final KVP data; // The key value pair associated with this node   public ListNode next;  // Reference to any child node    /// Parameterized constructor forces users of the class to provide both   /// key and value on creation.   /// @param key the unique identifier for this node. immutable   /// @param value the value associated with the provided key   public ListNode(String key, String value) {     data = new KVP(key, value);     next = null;   }    /// Accessor to retrieve the key from the key-value pair   /// @return the unique key stored in this node   public String getKey() {     return data.key;   }    /// Accessor to retrieve the value from the key-value pair   /// @return the value stored in this node   public String getValue() {     return data.asString();   } } 

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_2

Step: 3

blur-text-image_3

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

Transport Operations

Authors: Allen Stuart

2nd Edition

978-0470115398, 0470115394

Students also viewed these Programming questions