Question: Hi i need help implementing a hashmap with Linear Probing. Task : implement the Put method in the given code below in JAVA. ( If

Hi i need help implementing a hashmap with Linear Probing. Task : implement the Put method in the given code below in JAVA. ( If the slot if occupied dont not replace it. Instead traverse down the list until an empty slot is found.) Thanks.

CODE:

import java.util.Collections;

import java.util.List;

import java.util.ArrayList;

public class LinearHashMap implements Map {

private static class HashMapEntry implements Entry {

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 DEFUNCT = new HashMapEntry(null, null);

// The entry table

private List> entries;

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

}

/**

* 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.

}

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

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!