1. Engineering
  2. Computer Science
  3. i need the last function for the following in java...

Question: i need the last function for the following in java...

Question details

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;
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution