|
| 1 | +package HashTable; |
| 2 | + |
| 3 | +import java.util.Random; |
| 4 | +import java.util.function.Function; |
| 5 | + |
| 6 | +public class HashTable<K, V> { |
| 7 | + |
| 8 | + public static final int INITIAL_TABLE_SIZE = 10; |
| 9 | + public static final double LOAD_FACTOR = 0.5; |
| 10 | + public static final int BIGNUMBER = 3345; |
| 11 | + |
| 12 | + private HashNode[] array; |
| 13 | + private Function<K, Integer> foh; |
| 14 | + private int tableSize = INITIAL_TABLE_SIZE; |
| 15 | + private int size; |
| 16 | + private static int numCollisions = 0; |
| 17 | + private static int numResizes = 0; |
| 18 | + |
| 19 | + public HashTable() { |
| 20 | + array = new HashNode[tableSize]; |
| 21 | + CreateHashingFunction(); |
| 22 | + } |
| 23 | + |
| 24 | + public HashTable(Function<K, Integer> f) { |
| 25 | + array = new HashNode[INITIAL_TABLE_SIZE]; |
| 26 | + foh = f; |
| 27 | + } |
| 28 | + |
| 29 | + public float loadFactor() { |
| 30 | + return size / (float) tableSize; |
| 31 | + } |
| 32 | + |
| 33 | + public int indexOf(K key) { |
| 34 | + return foh.apply(key); |
| 35 | + } |
| 36 | + |
| 37 | + public void insert(K key, V val) { |
| 38 | + int index = indexOf(key); |
| 39 | + HashNode e = new HashNode(key, val); |
| 40 | + |
| 41 | + if (loadFactor() > LOAD_FACTOR) { |
| 42 | + //resize |
| 43 | + numResizes++; |
| 44 | + HashNode[] oldArray = array; |
| 45 | + array = new HashNode[tableSize * 2]; |
| 46 | + tableSize = tableSize * 2; |
| 47 | + for (int i = 0; i < oldArray.length; i++) { |
| 48 | + HashNode<K, V> node = oldArray[i]; |
| 49 | + while (node != null) { |
| 50 | + insert(node.getKey(), node.getValue()); |
| 51 | + node = node.getNext(); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (array[index] == null) { |
| 57 | + array[index] = e; |
| 58 | + } else { |
| 59 | + insert(array[index], e); |
| 60 | + numCollisions++; |
| 61 | + } |
| 62 | + size++; |
| 63 | + } |
| 64 | + |
| 65 | + public void insert(HashNode old, HashNode e) { |
| 66 | + if (old.getNext() == null) { |
| 67 | + old.setNext(e); |
| 68 | + } else { |
| 69 | + insert(old.getNext(), e); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public V get(K key) { |
| 74 | + int index = indexOf(key); |
| 75 | + HashNode<K, V> node = array[index]; |
| 76 | + while (node != null) { |
| 77 | + if (node.getKey().equals(key)) { |
| 78 | + return node.getValue(); |
| 79 | + } |
| 80 | + node = node.getNext(); |
| 81 | + } |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + public void delete(K key) { |
| 86 | + int index = indexOf(key); |
| 87 | + array[index] = null; |
| 88 | + } |
| 89 | + |
| 90 | + public int getSize() { |
| 91 | + return size; |
| 92 | + } |
| 93 | + |
| 94 | + public int getNumCollisions() { |
| 95 | + return numCollisions; |
| 96 | + } |
| 97 | + |
| 98 | + public int getNumResizes() { |
| 99 | + return numResizes; |
| 100 | + } |
| 101 | + |
| 102 | + private void CreateHashingFunction() { |
| 103 | + foh = (x) -> { |
| 104 | + Random r = new Random(); |
| 105 | + int a, b, p; |
| 106 | + a = r.nextInt(); |
| 107 | + b = r.nextInt(); |
| 108 | + p = nextPrime(BIGNUMBER); |
| 109 | + |
| 110 | + return Math.abs(((a * x.hashCode() + b * x.hashCode()) * p) % tableSize); |
| 111 | + }; |
| 112 | + } |
| 113 | + |
| 114 | + public void print() { |
| 115 | + for (HashNode item : array) { |
| 116 | + if (item != null) { |
| 117 | + System.out.print(item); |
| 118 | + print(item.getNext()); |
| 119 | + } else { |
| 120 | + System.out.println("x"); |
| 121 | + } |
| 122 | + System.out.println(); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public void print(HashNode e) { |
| 127 | + if (e == null) { |
| 128 | + return; |
| 129 | + } |
| 130 | + System.out.print(" - " + e); |
| 131 | + } |
| 132 | + |
| 133 | + /* helpers */ |
| 134 | + private static int nextPrime(int a) { |
| 135 | + while (!isPrime(a)) { |
| 136 | + a++; |
| 137 | + } |
| 138 | + return a; |
| 139 | + } |
| 140 | + |
| 141 | + private static boolean isPrime(int a) { |
| 142 | + if (a == 2) { |
| 143 | + return true; |
| 144 | + } else if (a % 2 == 0) { |
| 145 | + return false; |
| 146 | + } else { |
| 147 | + int i = 3, end = (int) Math.sqrt(a + 0.0); |
| 148 | + while (a % i != 0 && i <= end) { |
| 149 | + i = i + 2; |
| 150 | + } |
| 151 | + return i > end; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + public static void main(String[] args) { |
| 156 | + HashTable<String, String> ht = new HashTable<>(x -> Math.abs(x.hashCode()) % INITIAL_TABLE_SIZE); |
| 157 | + ht.insert("banana", "yellow"); |
| 158 | + ht.insert("apple", "green"); |
| 159 | + ht.insert("android", "green"); |
| 160 | + ht.insert("cat", "white"); |
| 161 | + ht.insert("body", "black"); |
| 162 | + |
| 163 | + ht.print(); |
| 164 | + |
| 165 | + System.out.println("----------- Get -----------"); |
| 166 | + System.out.println("apple : " + ht.get("apple")); |
| 167 | + |
| 168 | + |
| 169 | + ht.delete("apple"); |
| 170 | + ht.print(); |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments