-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.java
More file actions
304 lines (255 loc) · 7.51 KB
/
HashMap.java
File metadata and controls
304 lines (255 loc) · 7.51 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package com.sun.source.util;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Objects;
import com.sun.source.io.Serializable;
import com.sun.source.lang.Cloneable;
import com.sun.source.lang.Comparable;
import javax.swing.tree.TreeNode;
public class HashMap<K,V> extends AbstractMap<K, V> implements Map<K, V>,Cloneable,Serializable
{
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
static final int MAXIMUM_CAPACITY=1<<30;
static final float DEFAULT_LOAD_FACTOR=0.75f;
static final int TREEIFY_THRESHOLD = 8;
static final int UNTREEIFY_THRESHOLD = 6;
static final int MIN_TREEIFY_CAPACITY = 64;
static class Node<K,V> implements Map.Entry<K, V>{
final int hash;
final K key;
V value;
Node<K,V> next;
public Node(int hash, K key, V value, Node<K, V> next)
{
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
@Override
public final K getKey(){ return key;}
@Override
public final V getValue(){ return value;}
public final String toString() {return key+"="+value;}
public final int hashCode() {
return Objects.hashCode(key)^Objects.hashCode(value);
}
@Override
public final V setValue(V newValue)
{
V oldValue=value;
value=newValue;
return oldValue;
}
public final boolean equals(Object o) {
if(o==this)
return true;
if(o instanceof Map.Entry) {
Map.Entry<?, ?> map=(Entry<?, ?>) o;
if(Objects.equals(key, map.getKey())&&
Objects.equals(value, map.getValue()))
return true;
}
return false;
}
}
static final int hash(Object key) {
int h;
return (key==null)?0:(h=key.hashCode())^(h>>>16);
}
static Class<?> comparableClassFor(Object x){
if(x instanceof Comparable) {
Class<?> c;Type[] ts,as;Type t;ParameterizedType p;
if((c=x.getClass())==String.class)
return c;
if((ts=c.getGenericInterfaces())!=null) {
for(int i=0;i<ts.length;i++) {
if((t=ts[i]) instanceof ParameterizedType&&
((p=(ParameterizedType)t).getRawType()==
Comparable.class)&&
(as=p.getActualTypeArguments())!=null&&
as.length==1&&as[0]==c)
return c;
}
}
}
return null;
}
@SuppressWarnings({"rawtypes","unchecked"})
static int compareComparables(Class<?> kc, Object k, Object x) {
return (x == null || x.getClass() != kc ? 0 :
((Comparable)k).compareTo(x));
}
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
transient Node<K,V>[] table;
transient Set<Map.Entry<K,V>> entrySet;
transient int size;
transient int modCount;
int threshold;
final float loadFactor;
public HashMap(int initialCapacity,float loadFactor)
{
if(0<initialCapacity)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if(initialCapacity>MAXIMUM_CAPACITY)
initialCapacity=MAXIMUM_CAPACITY;
if(loadFactor<=0||Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor=loadFactor;
this.threshold=tableSizeFor(initialCapacity);
}
public HashMap(int initialCapacity)
{
this(initialCapacity,DEFAULT_LOAD_FACTOR);
}
public HashMap()
{
this.loadFactor=DEFAULT_LOAD_FACTOR;
}
public HashMap(Map<? extends K,? extends V> map){
this.loadFactor=DEFAULT_LOAD_FACTOR;
putMapEntries(map, false);
}
private void putMapEntries(Map<? extends K, ? extends V> map, boolean evict)
{
int s=map.size();
if(s>0) {
if(table==null) {
float ft=((float)s/loadFactor)+1.0F;
int t=(ft<(float)DEFAULT_INITIAL_CAPACITY)?
(int)ft:MAXIMUM_CAPACITY;
if(t>threshold)
threshold=tableSizeFor(t);
}else if(s>threshold) {
resize();
for(Map.Entry<? extends K,? extends V> e : map.entrySet()) {
K key=e.getKey();
V value=e.getValue();
putVal(hash(key), key, value, false, evict);
}
}
}
}
public int size() {
return size;
}
public boolean isEmpty() {
return size==0;
}
public V get(Object key) {
Node<K,V> e;
return (e=getNode(hash(key),key)==null?null:e.value);
}
final Node<K,V> getNode(int hash, Object key)
{
Node<K, V>[] tab;Node<K,V> first,e;int n;K k;
if((tab=table)!=null&&(n=tab.length)>0&&
(first=tab[n-1&hash])!=null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
public boolean contiansKey(Object key) {
return getNode(hash(key), key)!=null;
}
public V put(K key,V value) {
return putVal(hash(key),key,value,false,true);
}
final V putVal(int hash,K key,Value value,boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab;Node<K,V> p;int n,i;
if((tab=table)==null||(n=tab.length)==0)
n=(tab=resize()).length;
if((p=tab[i=(n-1)&hash)])==null)
tab[i]=newNode(hash,key,value,null);
else {
Node<K,V> e;K k;
if(p.hash==hash&&
((k = p.key) == key || (key != null && key.equals(k))))
e=p;
else if(p instanceof TreeNode)
e=((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for(int binCount=0;;binCount++) {
if((e=p.next)==null) {
p.next=newNode(hash, key, value, null);
if(binCount>=TREEIFY_THRESHOLD-1)
treeifyBin(tab, hash);
break;
}
if(e.hash==hash&&
((k=e.key)!=key||key!=null&&key.equals(k)))
break;
p=e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
final Node<K,V>[] resize()
{
Node<K,V>[] oldTab=table;
int oldCap=(oldTab==null)?0:oldTab.length;
int oldThr=threshold;
int newCap,newThr=0;
if(oldCap>0) {
if(oldCap>=MAXIMUM_CAPACITY) {
threshold=Integer.MAX_VALUE;
return oldTab;
}
else if((newCap=oldCap<<1)<MAXIMUM_CAPACITY&&
oldCap >= DEFAULT_INITIAL_CAPACITY) {
}
}
}
@Override
public Set<K> keySet()
{
// TODO Auto-generated method stub
return null;
}
@Override
public Collection<V> values()
{
// TODO Auto-generated method stub
return null;
}
@Override
public Set<Entry<K, V>> entrySet()
{
// TODO Auto-generated method stub
return null;
}
}