Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the following HashMapeK, V> class add: A method (void deletelk key) which

ID: 3738582 • Letter: U

Question

Using the following HashMapeK, V> class add: A method (void deletelk key) which deletes the key-value pair with the given key from the map if it is present. Note: Do not implement map resizing at this point. public class HashMapeK, Vs implements JRK, > private static final ?DE DEFAULT-NUM-BUCKETS 13; a Linked List of list of key-value pairs private Linked ListeKVPCK, V>21 buckets; private UEN; private int factor; public MutashMaalot M, iot factor] thisfacter- factor; this.bucket = new LinkedList[ M ]; for int i-ois bucket&Lenstbi; buckets[i] = new LinkedList

Explanation / Answer


package com.chegg_hashmap;
import java.util.LinkedList;
public class Hashmap_Linkedlist<K, V> implements IKVStore<K, V> {
public static final int DEFAULT_NUM_BUCKETS = 13;
// A Linked List of list of key-value pairs
private LinkedList<KVP<K, V>>[] buckets;
private int N;
private int factor;
public void MyHashMap(int M, int factor) {
this.factor = factor;
this.buckets = new LinkedList[M];
for (int i = 0; i< buckets.length; i++) {
buckets[i] = new LinkedList<>();
}
N=0;
}

public MyHashMap() {
this(DEFAULT_NUM_BUCKETS, 2);
}
public void delete (k key){

// The deletion code is shown below
}

-------------ANSWER------------



// The delete function that need to be included in the given linkedlist program.

public void delete(K key) {
int BUC_INDEX = hash(key.hashCode());
  
if (table[BUC_INDEX] != null) {
LinkedList<Entry<K, V>> buckets = table[BUC_INDEX];
for (Entry<K, V> entry: buckets)
if (entry.getKey().equals(key)) {
buckets.remove(entry);
break;
}
}
}


package com.chegg_hashmap;
import java.util.LinkedList;
public class Hashmap_Linkedlist<K, V> implements IKVStore<K, V> {
public static final int DEFAULT_NUM_BUCKETS = 13;
// A Linked List of list of key-value pairs
private LinkedList<KVP<K, V>>[] buckets;
private int N;
private int factor;
public void MyHashMap(int M, int factor) {
this.factor = factor;
this.buckets = new LinkedList[M];
for (int i = 0; i< buckets.length; i++) {
buckets[i] = new LinkedList<>();
}
N=0;
}

public MyHashMap() {
this(DEFAULT_NUM_BUCKETS, 2);
}
public void delete (k key){

// The deletion code is shown below
}

-------------ANSWER------------



// The delete function that need to be included in the given linkedlist program.

public void delete(K key) {
int BUC_INDEX = hash(key.hashCode());
  
if (table[BUC_INDEX] != null) {
LinkedList<Entry<K, V>> buckets = table[BUC_INDEX];
for (Entry<K, V> entry: buckets)
if (entry.getKey().equals(key)) {
buckets.remove(entry);
break;
}
}
}