-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementHashMap_706.java
More file actions
64 lines (59 loc) · 1.72 KB
/
Copy pathImplementHashMap_706.java
File metadata and controls
64 lines (59 loc) · 1.72 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
package Design;
import java.util.*;
public class ImplementHashMap_706 {
private class Pair{
private int key;
private int value;
Pair(int key,int value){
this.key=key;
this.value=value;
}
public int getKey(){return key;}
public int getValue(){return value;}
public void setValue(int value){this.value=value;}
}
private final int BASE=997;
private List<Pair>[] data;
private int hash(int key){
return key&BASE;
}
public ImplementHashMap_706() {
data=new List[BASE];
for(int i=0;i<BASE;i++){
data[i]=new LinkedList<>();
}
}
/** value will always be non-negative. */
public void put(int key, int value) {
int h=hash(key);
for (Pair pair : data[h]) {
if (pair.getKey() == key) {
pair.setValue(value);
return;
}
}
data[h].add(new Pair(key,value));
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
public int get(int key) {
int h=hash(key);
for (Pair pair : data[h]) {
if (pair.getKey() == key) {
return pair.getValue();
}
}
return -1;
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
public void remove(int key) {
int h=hash(key);
Iterator<Pair> iterator = data[h].iterator();
while (iterator.hasNext()){
Pair pair = iterator.next();
if(pair.getKey()==key){
iterator.remove();
return;
}
}
}
}