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
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
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
table = new SplayList[capacity];
for (int i = 0; i < table.length; i++) {
table[i] = new SplayList<>();
}
for (SplayList
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
return new Iterator
private int index = 0;
private 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
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
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