Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

}

/**

* 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions

Question

Compute the convolution y(n) of thesignals a". -3

Answered: 1 week ago

Question

Describe the patterns of business communication.

Answered: 1 week ago

Question

How was their resistance overcome?

Answered: 1 week ago