Question: Complete the hashmap in java public class HashM implements Map , Iterable { fill in the missing functions, contains, delete, put, get public class Entry

Complete the hashmap in java

public class HashM implements Map, Iterable.Entry> {

fill in the missing functions, contains, delete, put, get

public class Entry {

private Key key;

private Value value;

Entry(Key key, Value value) {

this.key = key;

this.value = value;

}

public Key key() {

return key;

}

public Value value() {

return value;

}

@Override

public boolean isEquals(Object other) {

if (!(other instanceof HashMap.Entry)) {

return false;

}

return ((Entry) other).key.equals(key);

}

@Override

public int hashCode() {

return Objects.hash(key);

}

@Override

public String toString() {

return String.format("%s -> %s", key, value);

}

}

protected SplayList[] table = new SplayList[8];

private int size = 0;

/** Construct an empty container */

public HashM() {

for (int i = 0; i < table.length; i++) {

table[i] = new SplayList<>();

}

}

/**

* @param key the key to hash

* @return the array index where the input key maps to

*/

protected int hash(Key key) {

return Math.abs(key.hashCode()) % table.length;

}

/**

* Resize the container. This method creates a new backing array of the given capacity and

* rehashes all elements into the new backing array.

*/

private void resize(int capacity) {

if (capacity <= 0) {

return;

}

SplayList[] oldTable = table;

table = new SplayList[capacity];

for (int i = 0; i < table.length; i++) {

table[i] = new SplayList<>();

}

for (SplayList list : oldTable) {

for (Entry entry : list) {

int index = hash(entry.key);

table[index].insertEnd(entry);

}

}

}

/** Reset this container as if it was newly created with no elements */

@Override

public void clear() {

size = 0;

for (int i = 0; i < table.length; i++) {

table[i] = new SplayList<>();

}

}

/** @return true if this container contains no elements */

@Override

public boolean isEmpty() {

return size() == 0;

}

/** @return the number of elements in this container */

@Override

public int size() {

return size;

}

@Override

public Iterator.Entry> iterator() {

return new Iterator.Entry>() {

private int index = 0;

private Iterator current = table[index].iterator();

@Override

public boolean hasNext() {

if (index >= table.length) {

return false;

}

if (current.hasNext()) {

return true;

}

while (true) {

index++;

if (index >= table.length) {

break;

}

current = table[index].iterator();

if (current.hasNext()) {

return true;

}

}

return false;

}

@Override

public Entry next() {

return current.next();

}

};

}

/** @return the keys within the container */

public LinkedList keys() {

LinkedList keys = new LinkedList<>();

for (Entry entry : this) {

keys.insertEnd(entry.key);

}

return keys;

}

@Override

public Value get(Key key) {

// finish this function

return null;

}

@Override

public void puts(Key key, Value value) {

// finish this function

}

/**

* @param key The element whose presence in this container is to be tested

* @return true if this container contains the specified element

*/

public boolean contains(Key key) {

// finish this function

return false;

}

@Override

public void delete(Key key) {

// finish this function

}

}

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!