|
| 1 | +package custom.hashtable; |
| 2 | + |
| 3 | +import lombok.Getter; |
| 4 | + |
| 5 | +/** |
| 6 | + * Hashtable implementation similar to <code>java.util.HashMap</code> {@link java.util.HashMap}. |
| 7 | + * @author Suyash |
| 8 | + */ |
| 9 | +public class CustomHashtable<K, V> { |
| 10 | + /** |
| 11 | + * Each position in this array will represent a bucket, And this whole array will serve |
| 12 | + * as a hashtable. |
| 13 | + */ |
| 14 | + private Node<K,V> table[]; |
| 15 | + |
| 16 | + /** |
| 17 | + * Total number of buckets available in this hashtable, which is equal to <code>table.length</code> |
| 18 | + */ |
| 19 | + private Integer capacity; |
| 20 | + |
| 21 | + /** |
| 22 | + * Total number of elements present in the hashtable at any moment. |
| 23 | + */ |
| 24 | + @Getter |
| 25 | + private Integer size; |
| 26 | + |
| 27 | + public CustomHashtable(Integer capacity){ |
| 28 | + this.capacity = capacity; |
| 29 | + table = new Node[capacity]; |
| 30 | + size = 0; |
| 31 | + } |
| 32 | + |
| 33 | + public V put(K key,V value){ |
| 34 | + // Fetching the bucket(i.e. index) in which this element(key, value pair) needs to be stored. |
| 35 | + int index = getIndex(key); |
| 36 | + |
| 37 | + // Creating a new node. |
| 38 | + Node<K,V> newNode = new Node<K,V>(key, value, null); |
| 39 | + |
| 40 | + // Fetching the first element present(stored) in this bucket, if bucket is not empty. otherwise storedNode will be null. |
| 41 | + Node<K,V> storedNode = table[index]; |
| 42 | + |
| 43 | + // If target bucket is empty, Just store this element as a first element in this bucket. |
| 44 | + if(storedNode == null){ |
| 45 | + table[index] = newNode; |
| 46 | + } else { |
| 47 | + // If target bucket is not empty then iterate through all the elements in this bucket in order to check if the this is a duplicate element(key). |
| 48 | + for(Node<K,V> i=storedNode; i!=null; i=i.next){ |
| 49 | + // If this is duplicate element(key), replace the older value with newer value for this key. |
| 50 | + if(i.key.equals(key)){ |
| 51 | + V oldVal = i.value; |
| 52 | + i.value = value; |
| 53 | + size++; |
| 54 | + return oldVal; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // If we came here, Which means that no such element was there and just insert this element as a first element in target bucket |
| 59 | + // in the form of linked list and adjust the link. |
| 60 | + table[index] = newNode; |
| 61 | + newNode.next = storedNode; |
| 62 | + } |
| 63 | + size++; |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + public V get(K key){ |
| 68 | + int index = getIndex(key); |
| 69 | + Node<K,V> storedNode = table[index]; |
| 70 | + for(Node<K,V> i=storedNode; i!=null; i=i.next){ |
| 71 | + if(i.key.equals(key)) return i.value; |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + public void remove(K key){ |
| 77 | + // Fetching the bucket(i.e. index) in which this element(key, value pair) needs to be stored. |
| 78 | + int index = getIndex(key); |
| 79 | + |
| 80 | + // Fetching the first element present(stored) in this bucket, if bucket is not empty. otherwise storedNode will be null. |
| 81 | + Node<K,V> storedNode = table[index]; |
| 82 | + |
| 83 | + if(storedNode!=null) { |
| 84 | + Node<K,V> prevStoredNode = null; |
| 85 | + |
| 86 | + // Iterate through all the elements in this bucket until we found the match, Or all the elements have been visited. |
| 87 | + while (storedNode.next != null && !storedNode.key.equals(key)) { |
| 88 | + prevStoredNode = storedNode; |
| 89 | + storedNode = storedNode.next; |
| 90 | + } |
| 91 | + |
| 92 | + if (storedNode.key.equals(key)) { |
| 93 | + |
| 94 | + // If we found the match at the first position inside target bucket (Note-above while loop didn't run in this case.) |
| 95 | + if (prevStoredNode == null) { |
| 96 | + table[index] = storedNode.next; |
| 97 | + } else { |
| 98 | + prevStoredNode.next = storedNode.next; |
| 99 | + } |
| 100 | + size--; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public String toString() { |
| 107 | + String str=""; |
| 108 | + for(int i=0; i<table.length; i++){ |
| 109 | + str = str+"\nBucket-"+i+" : "; |
| 110 | + for(Node<K,V> j=table[i]; j!=null; j=j.next){ |
| 111 | + str = str + j.value+" "; |
| 112 | + } |
| 113 | + } |
| 114 | + return str; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * |
| 119 | + * @param key |
| 120 | + * @return bucket number corresponding to this key. Bucket number is calculated on the basis of hashcode of <code>key</code>. |
| 121 | + */ |
| 122 | + private int getIndex(K key){ |
| 123 | + int hashCode = key.hashCode(); |
| 124 | + int index = hashCode % capacity; |
| 125 | + return index; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * An instance of this class represents a node/element. |
| 130 | + * Elements are distinguished on the basis of their <code>key</code>. |
| 131 | + */ |
| 132 | + static class Node<K, V>{ |
| 133 | + final K key; |
| 134 | + V value; |
| 135 | + Node<K,V> next; |
| 136 | + |
| 137 | + Node(K key, V value, Node<K,V> next){ |
| 138 | + this.key = key; |
| 139 | + this.value = value; |
| 140 | + this.next = next; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments