Instructions
Objective
Write a program to implement hash and maps in java.
Requirements and Specifications
Source Code
import java.util.HashSet;
import java.util.Set;
public class MyHashMap {
// Holds an array of linked list of hash nodes
private Object[] nodes = new Object[8];
// Remove all elements
public void clear() {
for (int i = 0; i < nodes.length; i++) {
nodes[i] = null;
}
}
// Check if key exists
public boolean containsKey(K key) {
int index = hash(key);
if (nodes[index] == null) {
return false;
}
HashNode currentNode = (HashNode) nodes[index];
while (currentNode != null) {
if (currentNode.getKey().equals(key)) {
return true;
}
currentNode = currentNode.getNext();
}
return false;
}
// Check if value exists
public boolean containsValue(V value) {
for (Object object : nodes) {
if (object != null) {
HashNode currentNode = (HashNode) object;
while (currentNode != null) {
if (currentNode.getValue().equals(value)) {
return true;
}
currentNode = currentNode.getNext();
}
}
}
return false;
}
// Get the value associated with the key
public V get(K key) {
int index = hash(key);
if (nodes[index] == null) {
return null;
}
HashNode currentNode = (HashNode) nodes[index];
while (currentNode != null) {
if (currentNode.getKey().equals(key)) {
return currentNode.getValue();
}
currentNode = currentNode.getNext();
}
return null;
}
// Check if empty
public boolean isEmpty() {
for (Object object : nodes) {
if (object != null) {
return false;
}
}
return true;
}
// Return the set of keys
public Set keySet() {
Set set = new HashSet<>();
for (Object object : nodes) {
if (object != null) {
HashNode currentNode = (HashNode) object;
while (currentNode != null) {
set.add(currentNode.getKey());
currentNode = currentNode.getNext();
}
}
}
return set;
}
// Add or update a new pair
public void put(K key, V value) {
int index = hash(key);
// No collisions for fresh slot
if (nodes[index] == null) {
nodes[index] = new HashNode<>(key, value);
return;
}
// Search if the key already exists
HashNode currentNode = (HashNode) nodes[index];
while (currentNode != null) {
if (currentNode.getKey().equals(key)) {
// Do update ofkey already exists
currentNode.setValue(value);
return;
}
currentNode = currentNode.getNext();
}
// Do add in front
HashNode node = new HashNode<>(key, value);
node.setNext((HashNode) nodes[index]);
nodes[index] = node;
}
// Remove the key and returnthe associated value
public V remove(K key) {
int index = hash(key);
if (nodes[index] == null) {
return null;
}
HashNode previousNode = null;
HashNode currentNode = (HashNode) nodes[index];
while (currentNode != null) {
if (currentNode.getKey().equals(key)) {
if (currentNode == nodes[index]) {
// Case deleting the head
nodes[index] = ((HashNode) nodes[index]).getNext();
} else {
// Case deleting in-between
previousNode.setNext(currentNode.getNext());
}
return currentNode.getValue();
}
previousNode = currentNode;
currentNode = currentNode.getNext();
}
return null;
}
// Return the numberof elements
public int size() {
int overallSize = 0;
for (int i = 0; i < nodes.length; i++) {
overallSize += listSize(i);
}
return overallSize;
}
// Print out the table
public void printTable() {
int conflicts = 0;
for (int i = 0; i < nodes.length; i++) {
conflicts += printList(i);
}
System.out.println("Total # of conflicts: " + conflicts);
}
// Print the list content
private int printList(int index) {
System.out.print("Index " + index + ": ");
int totalConflicts = 0;
if (nodes[index] == null) {
System.out.println(" (0 conflicts), []");
} else {
int conflicts = listSize(index) - 1;
totalConflicts += conflicts;
System.out.print(" (" + conflicts + " conflicts), [");
HashNode currentNode = (HashNode) nodes[index];
while (currentNode != null) {
System.out.print(currentNode.getKey() + ", ");
currentNode = currentNode.getNext();
}
System.out.println("]");
}
return totalConflicts;
}
// Return how many elements are in the linked list
private int listSize(int index) {
if (nodes[index] == null) {
return 0;
}
int count = 0;
HashNode currentNode = (HashNode) nodes[index];
while (currentNode != null) {
count++;
currentNode = currentNode.getNext();
}
return count;
}
// Hash the key
private int hash(K key) {
int hashCode = key.hashCode();
int index = hashCode % nodes.length;
return Math.abs(index);
}
}
Similar Samples
Check out our programming homework samples to see the high standards we maintain. Each example demonstrates our ability to solve intricate coding problems in various languages. These samples highlight our expertise and dedication, helping you feel confident in choosing us for your programming assignments.
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C