-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHashmap.java
More file actions
169 lines (144 loc) · 4.34 KB
/
Copy pathHashmap.java
File metadata and controls
169 lines (144 loc) · 4.34 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
import java.util.*;
public class Hashmap {
private class Node {
Integer key = 0;
Integer value = 0;
Node next = null;
Node (Integer key, Integer value) {
this.key = key;
this.value = value;
}
}
private class linkedlist {
public Node head = null, tail = null;
public int noe = 0;
public int size() {
return noe;
}
public void addLast(Node node) {
if(head == null) {
head = tail = node;
} else {
tail.next = node;
tail = node;
}
noe ++;
}
public Node getFirst() {
return head;
}
public Node removeFirst() {
Node temp = head;
if(noe == 0) head = tail = null;
if(head != null) {
head = head.next;
}
noe --;
return temp;
}
}
private linkedlist[] containers;
public int sohm = 0;
public void assignValues(int size) {
containers = new linkedlist[size];
sohm = 0;
for(int i = 0; i < size; i ++) containers[i] = new linkedlist();
}
Hashmap() {
assignValues(10);
}
public void display() {
StringBuilder sb = new StringBuilder();
sb.append("[ ");
for(int i = 0; i < containers.length; i ++) {
linkedlist group = containers[i];
int size = group.size();
while(size --> 0) {
Node node = group.removeFirst();
sb.append("(" + node.key + "=" + node.value + ") ");
group.addLast(node);
}
}
sb.append("]");
System.out.println(sb.toString());
}
private void rehash(){
linkedlist[] backup = containers;
assignValues(2 * backup.length);
for(int i = 0; i < backup.length; i ++) {
linkedlist group = backup[i];
int size = group.size();
while(size --> 0) {
Node node = group.removeFirst();
put(node.key, node.value);
}
}
System.out.println("rehash called");
}
public void put(Integer key, Integer value) {
boolean isKey = containsKey(key);
if(isKey) {
linkedlist group = group(key);
group.head.value = value;
} else{
linkedlist group = group(key);
group.addLast(new Node(key, value));
sohm ++;
double lambda = group.size() / (containers.length * 1.0);
if (lambda > 0.6) rehash();
}
}
public void putIfAbsent (Integer key, Integer value) {
if(!containsKey(key)) put(key, value);
}
public ArrayList<Integer> keySet() {
ArrayList<Integer> keys = new ArrayList<>();
for(int i = 0; i < containers.length; i ++) {
linkedlist group = containers[i];
int size = group.size();
while(size --> 0) {
Node node = group.removeFirst();
keys.add(node.key);
group.addLast(node);
}
}
return keys;
}
public boolean isEmpty() {
return sohm == 0;
}
public Integer remove(Integer key) {
boolean isKey = containsKey(key);
if(!isKey)
return null;
linkedlist group = group(key);
Node rv = group.removeFirst();
sohm --;
return rv.value;
}
public int getOrDefault(Integer key, Integer def) {
return containsKey(key) ? get(key) : def;
}
public int get(Integer key) {
boolean isKey = containsKey(key);
linkedlist group = group(key);
return isKey ? group.head.value : null;
}
public boolean containsKey(Integer key) {
linkedlist group = group(key);
int size = group.size();
while(size --> 0) {
if(group.getFirst().key == key) return true;
group.addLast(group.removeFirst());
}
return false;
}
private linkedlist group(Integer key) { // return container alloted to a key;
int code = hashCode(key);
return containers[code];
}
private int hashCode(Integer key) {
int val = key.hashCode();
return val % containers.length;
}
}