-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGenericHashMap.java
More file actions
136 lines (114 loc) · 2.89 KB
/
GenericHashMap.java
File metadata and controls
136 lines (114 loc) · 2.89 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
package lecture9a22;
import java.lang.reflect.Array;
import java.util.ArrayList;
import javax.xml.ws.spi.http.HttpContext;
import lecture4.array;
import lecture9a21.GenericLinkedList;
public class GenericHashMap<K, V> {
private class HTPair {
K key;
V value;
HTPair(K key, V value) {
this.key = key;
this.value = value;
}
public boolean equals(Object other) {
HTPair op = (HTPair) other; /// how??
return this.key.equals(op.key);
}
public String toString() {
return "{" + this.key + "-" + this.value + "}";
}
}
public static final int DEFAULT_CAPACITY = 10;
private GenericLinkedList<HTPair>[] bucketArray;
private int size;
public GenericHashMap() {
this(DEFAULT_CAPACITY);/// means??
}
public GenericHashMap(int Capacity) {
this.bucketArray = new GenericLinkedList[Capacity];
this.size = 0;
}
public void put(K key, V value) throws Exception {
int bi = HashFunction(key);
HTPair np = new HTPair(key, value);
GenericLinkedList<HTPair> bucket = this.bucketArray[bi];
if (bucket == null) {
bucket = new GenericLinkedList<>();
bucket.addLast(np);
this.size++;
this.bucketArray[bi] = bucket;
} else {
int idx = bucket.find(np);
if (idx == -1) {
bucket.addLast(np);
this.size++;
} else {
HTPair getp = bucket.getAt(idx);
getp.value = value;
}
}
double lamda = (this.size*1.0)/this.bucketArray.length;
if(lamda>0.75)
rehash();
}
private void rehash() throws Exception {
GenericLinkedList<HTPair>[] oba = this.bucketArray;
this.bucketArray = new GenericLinkedList[2*oba.length];
this.size=0;
for(GenericLinkedList<HTPair> ob : oba) {
while(ob!=null && !ob.isEmpty()) {
HTPair getp = ob.removeFirst();
this.put(getp.key, getp.value);
}
}
}
private int HashFunction(K key) {
int hc = key.hashCode();
int bi = Math.abs(hc) % this.bucketArray.length;
return bi;
}
public void display() {
for (int i = 0; i < bucketArray.length; i++) {
GenericLinkedList<HTPair> bucket = bucketArray[i];
if (bucket == null || bucket.isEmpty())
System.out.println("NULL");
else
bucket.display();
}
System.out.println("----------------------------------");
}
public V get(K key) throws Exception {
int bi = HashFunction(key);
HTPair np = new HTPair(key, null);
GenericLinkedList<HTPair> bucket = this.bucketArray[bi];
if (bucket == null) {
return null;
} else {
int idx = bucket.find(np);
if (idx == -1) {
return null;
} else {
HTPair getp = bucket.getAt(idx);
return getp.value;
}
}
}
public V remove(K key) throws Exception {
int bi = HashFunction(key);
HTPair np = new HTPair(key, null);
GenericLinkedList<HTPair> bucket = this.bucketArray[bi];
if (bucket == null) {
return null;
} else {
int idx = bucket.find(np);
if (idx == -1) {
return null;
} else {
this.size--;
return (V) bucket.removeAt(idx);
}
}
}
}