×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Create a Program to Implement Hash and Maps in Java Assignment Solution.

June 15, 2024
Sarah Williams
Sarah Williams
🇨🇦 Canada
C
Sarah is a skilled C programmer with a bachelor's degree in computer science and over 600 completed orders. Specializing in data structure implementation, she delivers meticulously crafted solutions that leverage arrays, linked lists, trees, and hash tables for effective mapping operations.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Use Python libraries effectively by importing only what you need. For example, if you're working with data, using libraries like pandas and numpy can save time and simplify complex tasks like data manipulation and analysis.
News
In 2024, the Biden-Harris Administration has expanded high-dosage tutoring and extended learning programs to boost academic achievement, helping programming students and others recover from pandemic-related setbacks. These initiatives are funded by federal resources aimed at improving math and literacy skills​

Instructions

Objective

Write a program to implement hash and maps in java.

Requirements and Specifications

program-to-implement-hash-and-maps-in-java (2)

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.