Question
Implement a hash map with LINEAR PROBING. Implement the following methods (Method with comments TODO) in the code in JAVA . Thanks. CODE: import java.util.Collections;
Implement a hash map with LINEAR PROBING. Implement the following methods (Method with comments TODO) in the code in JAVA . Thanks.
CODE:
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class LinearHashMap
private static class HashMapEntry
private K key;
private V value;
public HashMapEntry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
public void setKey(K key) {
this.key = key;
}
public void setValue(V value) {
this.value = value;
}
}
private static final int DEFAULT_CAPACITY = 16;
// Use this entry to signify defunct cells.
private final HashMapEntry
// The entry table
private List
// Actual number of entries in map
private int numberOfEntries;
public LinearHashMap() {
entries = new ArrayList<>(Collections.nCopies(DEFAULT_CAPACITY, null));
numberOfEntries = 0;
}
private int hash(K key) {
int h = key.hashCode() % entries.size();
if (h < 0) {
h += 32;
}
return h;
}
@Override
public int size() {
return numberOfEntries;
}
@Override
public boolean isEmpty() {
return size() == 0;
}
/**
* Tests whether the map contains the given key.
*
* @param k
* a key
* @return true if the map contains the given key, false otherwise
*/
@Override
public boolean containsKey(K key) {
// TODO implement method
return false;
}
/**
* Returns the value associated with the given key if any.
*
* @param k
* a key
* @return the value associated with the given key (or null if none present)
*/
@Override
public V get(K key) {
// TODO Implement method
return null;
}
/**
* Inserts an entry into the map with the given key and value.
*
* @param k
* a key
* @param v
* a value
*/
@Override
public void put(K key, V value) {
// TODO: Implement method.
}
/**
* Removes the entry associated with the given key if any, and returns the corresponding value.
*
* @param k
* a key
* @return the value from the removed entry (or null if none present)
*/
@Override
public V remove(K key) {
// TODO: Implement method.
return null;
}
public static void main(String[] args) {
System.out.println("Running the main method in LinearHashMap.java");
}
}
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