Design HashMap without using any built-in hash table libraries.
To be specific, your design should include these functions:
put(key, value)
: Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value.get(key)
: Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.remove(key)
: Remove the mapping for the value key if this map contains the mapping for the key.
Example
put(1,1) :
put(1,2) :
Get(1): 2
Get(1): -1
Approach
Java
import java.util.Arrays;public class HashMapTest {public static void main(String[] args) {HashMapOwn hashMapOwn = new HashMapOwn();System.out.println("put(1,1) : ");hashMapOwn.put(1, 1);System.out.println("put(1,2) : ");hashMapOwn.put(1, 2);System.out.println("Get(1): " + hashMapOwn.get(1));hashMapOwn.remove(1);System.out.println("Get(1): " + hashMapOwn.get(1));System.out.println("put(1,1) : ");hashMapOwn.put(1, 1);System.out.println("put(2,2) : ");hashMapOwn.put(2, 2);System.out.println("put(3,3) : ");hashMapOwn.put(3, 3);System.out.println("put(4,4) : ");hashMapOwn.put(4, 4);System.out.println("put(5,5) : ");hashMapOwn.put(5, 5);}}class HashMapOwn {int map[];public HashMapOwn() {map = new int[1000000];Arrays.fill(map, -1);}public void put(int key, int value) {map[key] = value;}public int get(int key) {return map[key];}public void remove(int key) {map[key] = -1;}}
C++
#include <bits/stdc++.h>using namespace std;vector<int> hashMap;//function to add the value//at the given keyvoid put(int key, int value){hashMap[key]=value;}//get the value at given//keyint get(int key){return hashMap[key];}//remove the element from//the hashMapvoid remove(int key){hashMap[key]=-1;}int main(){hashMap.clear();hashMap.resize(1000001,-1);put(1,1);put(1,2);cout<<get(1)<<"\n";put(2,3);cout<<get(2)<<"\n";remove(1);return 0;}
Approach: Effective approach
Java
public class HashMapTest {public static void main(String[] args) {HashMapOwn hashMapOwn = new HashMapOwn(4);System.out.println("put(1,1) : ");hashMapOwn.put(1, 1);System.out.println("put(1,2) : ");hashMapOwn.put(1, 2);System.out.println("Get(1): " + hashMapOwn.get(1));hashMapOwn.remove(1);System.out.println("Get(1): " + hashMapOwn.get(1));System.out.println("put(1,1) : ");hashMapOwn.put(1, 1);System.out.println("put(2,2) : ");hashMapOwn.put(2, 2);System.out.println("put(3,3) : ");hashMapOwn.put(3, 3);System.out.println("put(4,4) : ");hashMapOwn.put(4, 4);System.out.println("Size(): " + hashMapOwn.size());System.out.println("put(5,5) : ");hashMapOwn.put(5, 5);System.out.println("Size(): " + hashMapOwn.size());System.out.println("Iterate hash map : ");for (Pair<Integer, Integer> p : hashMapOwn.map) {System.out.println("Key " + p.getKey() + " Value " + p.getValue());}}}class HashMapOwn {Pair<Integer, Integer> map[];private int capacity = 4; // Initial capacity of HashMapint top = 0;@SuppressWarnings("unchecked")public HashMapOwn() {map = new Pair[capacity];}@SuppressWarnings("unchecked")public HashMapOwn(int capacity) {map = new Pair[capacity];this.capacity = capacity;}public void put(int key, int value) {// calculate hash of key.int hash = hash(key);if (top <= capacity) {top++;map[hash] = new Pair(key, value);} else {System.out.print("Capacity Full !!Can not add new " + value + " ");}}public int size() {return map.length;}public int get(int key) {// calculate hash of key.int hash = hash(key);int k = map[hash] == null ? -1 : map[hash].getValue();if (k > -1)top--;return k;}public void remove(int key) {// calculate hash of key.int hash = hash(key);int k = map[hash] == null ? -1 : map[hash].getKey();if (k != -1) {map[hash] = null;}}// Calculate the hashcode for// finding appropriate bucketpublic int hash(int key) {return Math.abs(key + "".hashCode()) % capacity;}}@SuppressWarnings("rawtypes")class Pair<F extends Comparable<F>, S extendsComparable<S>> implements Comparable<Pair> {private F key;private S value;public Pair() {}public Pair(F key, S value) {this.key = key;this.value = value;}public F getKey() {return key;}public void setKey(F key) {this.key = key;}public S getValue() {return value;}public void setValue(S value) {this.value = value;}@SuppressWarnings("unchecked")@Overridepublic int compareTo(Pair o) {return getKey().compareTo((F) o.getValue());}}
No comments:
Post a Comment