forked from christine-1017/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHashMap.java
More file actions
164 lines (147 loc) · 3.33 KB
/
MyHashMap.java
File metadata and controls
164 lines (147 loc) · 3.33 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
/**
* HashMap implementation.
* Using separate chaining to handle collision.
*
* @author wish
*
* @param <K> KEY
* @param <V> VALUE
*/
public class MyHashMap<K, V> {
static class Entry<K, V> {
final K key;
V value;
Entry<K,V> next;
Entry(final K key, final V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public void setValue(final V value) {
this.value = value;
}
}
private static final int DEFAULT_CAPACITY = 16;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private static final int MAXIMUM_CAPACITY = 1 << 30;
private Entry<K, V>[] array;
private int size;
private float loadFactor;
public MyHashMap() {
this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
}
public MyHashMap(final int capacity, final float loadFactor) {
if (capacity <= 0) {
throw new IllegalArgumentException("Capacity must be a positive number");
}
this.array = (Entry<K, V>[]) (new Entry[capacity]);
this.size = 0;
this.loadFactor = loadFactor;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public boolean containsValue(final V value) {
if (isEmpty()) {
return false;
}
for (Entry<K, V> entry : array) {
while (entry != null) {
if (equalsValue(entry.getValue(), value)) {
return true;
}
entry = entry.next;
}
}
return false;
}
private boolean equalsValue(final V v1, final V v2) {
return v1 == v2 || (v1 != null && v1.equals(v2));
}
private int getIndex(final K key) {
return hash(key) % array.length;
}
private int hash(final K key) {
if (key == null) {
return 0;
}
return key.hashCode() & 0X7FFFFFFF;
}
private Entry<K, V> getEntry(final K key) {
if (isEmpty()) {
return null;
}
int index = getIndex(key);
Entry<K, V> entry = array[index];
while (entry != null) {
if (equalsKey(entry.getKey(), key)) {
return entry;
}
entry = entry.next;
}
return null;
}
private boolean equalsKey(final K key1, final K key2) {
return key1 == key2 || (key1 != null && key1.equals(key2));
}
public boolean containsKey(K key) {
return getEntry(key) != null;
}
public V put(K key, V value) {
Entry<K, V> entry = getEntry(key);
if (entry != null) {
V oldValue = entry.getValue();
entry.setValue(value);
return oldValue;
}
int index = getIndex(key);
Entry<K, V> head = new Entry<K, V>(key, value);
head.next = array[index];
array[index] = head;
size++;
if (needRehashing()) {
rehashing();
}
return null;
}
private boolean needRehashing() {
return size / array.length >= loadFactor;
}
private void rehashing() {
resize(2 * array.length);
}
private void resize(int newCapacity) {
Entry[] oldTable = array;
int oldCapacity = array.length;
if (oldCapacity == MAXIMUM_CAPACITY) {
return;
}
Entry[] newTable = new Entry[newCapacity];
transfer(newTable);
}
private void transfer(Entry[] newTable) {
Entry[] old = array;
int newCapacity = newTable.length;
for (int i = 0; i < old.length; i++) {
Entry<K, V> entry = old[i];
if (entry != null) {
old[i] = null;
while (entry != null) {
Entry<K, V> next = entry.next;
int index = getIndex(entry.getKey());
entry.next = newTable[i];
newTable[i] = entry;
entry = next;
}
}
}
}
}