Question
I need the last function for the following in Java: /** Array to store the start of each hash bucket list. */ private Item[] items;
I need the last function for the following in Java:
/** Array to store the start of each hash bucket list. */ private Item[] items;
/** * Create a new HashMap with the given size. * * @param setSize initial size of the new Hashmap */ HashMap(final int setSize) { items = new Item[setSize]; }
/** * Hash an object to a hash bucket index. * * @param key object to hash * @return hash bucket index to use */ private int hash(final Object key) { int hashValue = key.hashCode() % items.length; if (hashValue < 0) { hashValue += items.length; } return hashValue; }
/** * Put an item into this HashMap. * * If an item with this key exists, it should replace the value. * If it does not exist, it should be added if there is space. * The function returns true if the value was added correctly and false otherwise. * * @param key key of the item we're putting * @param value value of the item we're putting * * @return true if we were able to put this object into the HashMap, * or false if the HashMap is full */ public boolean put(final Object key, final Object value) { int bucket = hash(key); for (int i = 0; i < items.length; i++) { int currentBucket = (bucket + i) % items.length; if (items[currentBucket] == null) { items[currentBucket] = new Item(key, value); return true; } else if (items[currentBucket].key.equals(key)) { items[currentBucket].value = value; return true; } } return false; }
/** * Get the value matching a key from this HashMap. * * @param key key for the value to retrieve * @return value matching this key or null if no value matches this key */ public Object get(final Object key) { return null; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started