Repository navigation
Expand file tree
/
Copy pathMyHashtable.java
More file actions
164 lines (123 loc) · 4.01 KB
/
Copy pathMyHashtable.java
File metadata and controls
164 lines (123 loc) · 4.01 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.hellokoding.datastructure;
import java.util.Arrays;
import java.util.Objects;
public class MyHashtable<K, V> {
private static final int INITIAL_CAPACITY = 16;
private static final float LOAD_FACTOR = 0.75f;
private int size = 0;
Entry<K,V>[] entries;
@SuppressWarnings({"rawtypes", "unchecked"})
public MyHashtable() {
entries = (Entry<K, V>[]) new Entry[INITIAL_CAPACITY];
}
@SuppressWarnings({"rawtypes", "unchecked"})
public MyHashtable(int capacity) {
entries = (Entry<K, V>[]) new Entry[capacity];
}
public V put(K key, V value) {
int index = hashFunction(key);
Entry<K, V> headEntry = entries[index];
Entry<K, V> currentEntry = headEntry;
// Find key in the chain
while (Objects.nonNull(currentEntry)) {
if (Objects.equals(currentEntry.key, key)) {
// Ignore duplicate key
return currentEntry.value;
}
currentEntry = currentEntry.next;
}
// Add first to the chain
Entry<K, V> newEntry = new Entry<>(key, value);
newEntry.next = headEntry;
entries[index] = newEntry;
size++;
resize();
return null;
}
public V get(K key) {
int index = hashFunction(key);
Entry<K, V> currentEntry = entries[index];
// Find key in the chain
while (Objects.nonNull(currentEntry)) {
if (Objects.equals(currentEntry.key, key)) {
return currentEntry.value;
}
currentEntry = currentEntry.next;
}
return null;
}
public V remove(K key) {
int index = hashFunction(key);
Entry<K, V> currentEntry = entries[index];
Entry<K, V> previousEntry = null;
// Find key in the chain
while (Objects.nonNull(currentEntry)) {
if (Objects.equals(currentEntry.key, key)) {
break;
}
previousEntry = currentEntry;
currentEntry = currentEntry.next;
}
if (Objects.isNull(currentEntry)) {
return null;
}
// Remove
if (Objects.isNull(previousEntry)) {
entries[index] = currentEntry.next;
} else {
previousEntry.next = currentEntry.next;
}
size--;
return currentEntry.value;
}
public int size() {
return size;
}
private void resize() {
if (size <= LOAD_FACTOR * entries.length) return;
Entry<K,V>[] currentEntries = entries;
entries = (Entry<K, V>[]) new Entry[entries.length*2] ;
rehash(currentEntries);
}
private void rehash(Entry<K,V>[] entries) {
size = 0;
for (Entry<K, V> entry : entries) {
while (Objects.nonNull(entry)) {
put(entry.key, entry.value);
entry = entry.next;
}
}
}
private int hashFunction(K key) {
int hashCode = key.hashCode();
int index = hashCode % entries.length;
return index;
}
public static class Entry<K, V> {
K key;
V value;
Entry<K, V> next;
Entry(K key, V value) {
this.key = key;
this.value = value;
}
}
public static void main(String[] args) {
MyHashtable<String, Integer> myHashtable = new MyHashtable<>(2);
myHashtable.put("k1", 1);
myHashtable.put("k2", 2);
myHashtable.put("k2", 2);
myHashtable.put("k3", 3);
Arrays.stream(myHashtable.entries)
.forEach(e -> {
if (Objects.nonNull(e)) {
System.out.printf("%s=%d%s", e.key, e.value, System.lineSeparator());
}
});
System.out.println(myHashtable.size());
System.out.println(myHashtable.get("k2"));
System.out.println(myHashtable.remove("k1"));
System.out.println(myHashtable.get("k1"));
System.out.println(myHashtable.size());
}
}