Repository navigation
Expand file tree
/
Copy pathHashMapCode.java
More file actions
145 lines (123 loc) · 4.18 KB
/
Copy pathHashMapCode.java
File metadata and controls
145 lines (123 loc) · 4.18 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
import java.util.*;
public class HashMapCode {
static class HashMap<K, V> {
private class Node {
K key;
V value;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
}
private int n; // n--> nodes
private int N; // N--> buckets
private LinkedList<Node> buckets[]; //N=buckets.length
@SuppressWarnings("Unchecked")
public HashMap() {
this.N = 4;
this.buckets = new LinkedList[4];
for (int i = 0; i < 4; i++) {
this.buckets[i] = new LinkedList<>();
}
}
private int hashfunction(K key) {
int bi = key.hashCode();
return Math.abs(bi) % N;
}
private int searchinLL(K key, int bi) {
LinkedList<Node> ll = buckets[bi];
for (int i = 0; i < ll.size(); i++) {
if (ll.get(i).key == key) {
return i;
}
}
return -1;
}
@SuppressWarnings("Unchecked")
private void rehash() {
LinkedList<Node> oldbucket[] = buckets;
buckets = new LinkedList[N * 2];
for (int i = 0; i < N * 2; i++) {
buckets[i] = new LinkedList<>();
}
for (int i = 0; i < oldbucket.length; i++) {
LinkedList<Node> ll = oldbucket[i];
for (int j = 0; j < ll.size();j++){
Node node = ll.get(j);
put(node.key, node.value);
}
}
}
public void put(K key, V value) {
int bi = hashfunction(key); //bi =bucket index
int di = searchinLL(key, bi); //di =data index
if (di == -1) { // key doesn't exist
buckets[bi].add(new Node(key, value));
n++;
} else { // key exist
Node node = buckets[bi].get(di);
node.value = value;
}
double lambda = (double) n / N;
if (lambda > 2.0) {
rehash();
}
}
public boolean containsKey(K key) {
int bi = hashfunction(key); //bi =bucket index
int di = searchinLL(key, bi); //di =data index
if (di == -1) { // key doesn't exist
return false;
} else { // key exist
return true;
}
}
public V remove(K key) {
int bi = hashfunction(key); //bi =bucket index
int di = searchinLL(key, bi); //di =data index
if (di == -1) { // key doesn't exist
return null;
} else { // key exist
Node node = buckets[bi].remove(di);
n--;
return node.value;
}
}
public V get(K key) {
int bi = hashfunction(key); //bi =bucket index
int di = searchinLL(key, bi); //di =data index
if (di == -1) { // key doesn't exist
return null;
} else { // key exist
Node node = buckets[bi].get(di);
return node.value;
}
}
public ArrayList<K> keyset() {
ArrayList<K> keys = new ArrayList<>();
for (int i = 0; i < buckets.length; i++) { //bi
LinkedList<Node> ll = buckets[i];
for (int j = 0; j < ll.size(); j++) { //ki
Node node = ll.get(j);
keys.add(node.key);
}
}
return keys;
}
public boolean isEmpty() {
return n == 0;
}
}
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("INDIA", 190);
map.put("china", 200);
map.put("Usa", 190);
ArrayList<String> keys = map.keyset();
for (int i = 0; i < keys.size(); i++) {
System.out.println(keys.get(i) + " " + map.get(keys.get(i)));
}
map.remove("china");
System.out.println(map.get("china"));
}
}