Advertisement
javatechie

Custom HashMap using Java

Aug 18th, 2020
1,314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. public class Entry<K, V> {
  2.  
  3.     K key;
  4.     V value;
  5.     Entry<K, V> next;
  6.  
  7.     public Entry(K key, V value, Entry<K, V> next) {
  8.         this.key = key;
  9.         this.value = value;
  10.         this.next = next;
  11.     }
  12.  
  13.     /**Usually in HashMap Entry Looks like this
  14.      *
  15.      * ------------------------------------------
  16.      * |  Key  |  Value  | Next Entry reference |
  17.      * ------------------------------------------
  18.      * **/
  19.  
  20.  
  21.     public K getKey() {
  22.         return key;
  23.     }
  24.  
  25.     public void setKey(K key) {
  26.         this.key = key;
  27.     }
  28.  
  29.     public V getValue() {
  30.         return value;
  31.     }
  32.  
  33.     public void setValue(V value) {
  34.         this.value = value;
  35.     }
  36.  
  37.     public Entry<K, V> getNext() {
  38.         return next;
  39.     }
  40.  
  41.     public void setNext(Entry<K, V> next) {
  42.         this.next = next;
  43.     }
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50. /**HashMap**/
  51.  
  52. public class CustomHashMap<K, V> {
  53.  
  54.     private Entry<K, V>[] buckets;
  55.     private static final int capacity = 16;
  56.  
  57.     public CustomHashMap(Entry<K, V>[] buckets) {
  58.         this.buckets = new Entry[capacity];
  59.     }
  60.  
  61.     public void put(K key, V value) {
  62.  
  63.         //check if key is null because it does not allow to store null.
  64.         if (key == null) {
  65.             return;
  66.         }
  67.         //get the bucket count for key using hash function
  68.         int bucketCount = hash(key);
  69.         //create new Entry
  70.         Entry<K, V> newEntry = new Entry<K, V>(key, value, null);
  71.  
  72.         //check in same bucket whether already we have entry or not
  73.         //if no entry present then directly add this entry
  74.         if (buckets[bucketCount] == null) {
  75.             buckets[bucketCount] = newEntry;
  76.         } else {
  77.  
  78.             Entry<K, V> previous = null;
  79.             Entry<K, V> current = buckets[bucketCount];
  80.  
  81.             while (current != null) {
  82.                 //if key is same then replace the value
  83.                 if (current.getKey().equals(key)) {
  84.                     current.setValue(value);
  85.                     break;
  86.                 }
  87.                 previous = current;
  88.                 current = current.getNext();
  89.             }
  90.             if (previous != null)
  91.                 previous.setNext(newEntry);
  92.         }
  93.  
  94.     }
  95.  
  96.     public V get(K key) {
  97.         int hash = hash(key);
  98.         Entry<K, V> entry = buckets[hash];
  99.         if (entry.getKey().equals(key)) {
  100.             return entry.getValue();
  101.         } else {
  102.             return null;
  103.         }
  104.     }
  105.  
  106.     private int hash(K key) {
  107.         return key.hashCode() % capacity;
  108.     }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement