forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericHashMapUsingArray.java
127 lines (104 loc) · 3.16 KB
/
GenericHashMapUsingArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.thealgorithms.datastructures.hashmap.hashing;
import java.util.LinkedList;
// implementation of generic hashmaps using array of Linked Lists
public class GenericHashMapUsingArray<K, V> {
private int size; // n (total number of key-value pairs)
private LinkedList<Node>[] buckets; // N = buckets.length
private float lf = 0.75f;
public GenericHashMapUsingArray() {
initBuckets(16);
size = 0;
}
// load factor = 0.75 means if we need to add 100 items and we have added
// 75, then adding 76th item it will double the size, copy all elements
// & then add 76th item.
private void initBuckets(int n) {
buckets = new LinkedList[n];
for (int i = 0; i < buckets.length; i++) {
buckets[i] = new LinkedList<>();
}
}
public void put(K key, V value) {
int bucketIndex = hashFunction(key);
LinkedList<Node> nodes = buckets[bucketIndex];
for (Node node : nodes) { // if key present => update
if (node.key.equals(key)) {
node.value = value;
return;
}
}
// key is not present => insert
nodes.add(new Node(key, value));
size++;
if ((float) size / buckets.length > lf) {
reHash();
}
}
// tells which bucket to go to
private int hashFunction(K key) {
return Math.floorMod(key.hashCode(), buckets.length);
}
private void reHash() {
System.out.println("Rehashing!");
LinkedList<Node>[] old = buckets;
initBuckets(old.length * 2);
this.size = 0;
for (LinkedList<Node> nodes : old) {
for (Node node : nodes) {
put(node.key, node.value);
}
}
}
public void remove(K key) {
int bucketIndex = hashFunction(key);
LinkedList<Node> nodes = buckets[bucketIndex];
Node target = null;
for (Node node : nodes) {
if (node.key.equals(key)) {
target = node;
break;
}
}
nodes.remove(target);
size--;
}
public int size() {
return this.size;
}
public V get(K key) {
int bucketIndex = hashFunction(key);
LinkedList<Node> nodes = buckets[bucketIndex];
for (Node node : nodes) {
if (node.key.equals(key)) {
return node.value;
}
}
return null;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("{");
for (LinkedList<Node> nodes : buckets) {
for (Node node : nodes) {
builder.append(node.key);
builder.append(" : ");
builder.append(node.value);
builder.append(", ");
}
}
builder.append("}");
return builder.toString();
}
public boolean containsKey(K key) {
return get(key) != null;
}
public class Node {
K key;
V value;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
}
}