Question: // TO DO: add your implementation and JavaDocs. // ONLY need to implement these THREE METHODS: put(), get(), and delete() // Keep other code AS

// TO DO: add your implementation and JavaDocs.

// ONLY need to implement these THREE METHODS: put(), get(), and delete() // Keep other code AS IS but you will need to add JavaDocs

import java.util.Iterator;

public class ThreeTenHashMap { //each entry of the hash map would be a pair, //key of type K and value of type V private class Pair { private K key; private V value; public Pair(K key, V value){ this.key = key; this.value = value; } public K getKey(){ return key; } public V getValue(){ return value; } public void setKey(K key){ this.key = key; } public void setValue(V value){ this.value = value; }

@Override public String toString(){ return "<"+key.toString()+","+value.toString()+">"; }

@Override @SuppressWarnings("unchecked") public boolean equals(Object o) { // return true if two pairs have matching keys // i.e. <"Alice", 1> is considered as equal to <"Alice", 2> if(o instanceof Pair) { Pair pair = (Pair)o; return pair.key.equals(key); } return false; }

@Override public int hashCode(){ //hashCode is determined by key only return key.hashCode(); } }

// This HashMap implementation uses an array of ThreeTenDLList // where each list will be composed of Node> private ThreeTenDLList>[] buckets;

// will fix the capacity to 11 final static private int DEFAULT_CAPACITY = 11;

// track how many elements in HashMap private int size; @SuppressWarnings("unchecked") public ThreeTenHashMap() { buckets = (ThreeTenDLList>[])new ThreeTenDLList[DEFAULT_CAPACITY]; size = 0; }

public int size() { return size; }

private int capacity() { return buckets.length; }

private int getHash(K key) { return Math.abs(key.hashCode()); }

@Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i=0; i> list = buckets[i]; sb.append("["); if (list != null) { sb.append(list.listToString()); } sb.append("]"); if (i!=buckets.length-1) sb.append(",");

} return "{" + sb.toString() + "}"; }

//****************************************************** //******* BELOW THIS LINE IS YOUR CODE ******* //******************************************************

// ADD PRIVATE METHODS HERE IF NEEDED! // YOU CANNOT ADD MORE DATA MEMBERS public void put(K key, V value) { // mapping key to value in the hashmap // - do not change hashmap for null key or null value // - if key is new, add a new entry (key, value) // - if key is present, make sure (key, value) is in record // Note: Implement the hash table with separate chaining. // - when a new (key,value) pair is added, add it to the end (tail) of the chain // O(load) on average, and O(n) worst case }

public V get(K key) { // return the current mapping of key // if key is null or not present, return null // // O(load) on average, and O(n) worst case

//default return, remove or update as needed return null; }

public V delete(K key){

// return the current mapping of key from hashmap and delete it // -if key is null or not present, return null // // O(load) on average, and O(n) worst case //default return, remove or update as needed return null; }

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