-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomHashSet.java
More file actions
216 lines (173 loc) · 5.56 KB
/
Copy pathCustomHashSet.java
File metadata and controls
216 lines (173 loc) · 5.56 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
package CustomUtil.HashSet;
public class CustomHashSet<E> implements CustomHashSetInterface<E>{
private static final int DEFAULT_SIZE = 16;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private int capacity;
private final float loadFactor;
private Node<E>[] table;
private int size;
public CustomHashSet() {
this(DEFAULT_SIZE, DEFAULT_LOAD_FACTOR);
}
@SuppressWarnings("unchecked")
public CustomHashSet(int capacity, float loadFactor) {
this.capacity = capacity;
this.loadFactor = loadFactor;
this.table = (Node<E>[])(new Node[capacity]);
this.size = 0;
}
private static class Node<E> {
private E element;
Node<E> next;
public Node(E element) {
this(element, null);
}
public Node(E element, Node<E> next) {
this.element = element;
this.next = next;
}
public E getElement() {
return this.element;
}
}
private int hash(E element) {
return element == null ? 0 : Math.abs(element.hashCode() % capacity);
}
private int hash(E element, int newCapacity) {
return element == null ? 0 : Math.abs(element.hashCode() % newCapacity);
}
@SuppressWarnings("unchecked")
private void resize() {
int newCapacity = capacity * 2;
Node<E>[] newTable = (Node<E>[]) (new Node[newCapacity]);
for (int i = 0; i < capacity; ++i) {
Node<E> currNode = table[i];
while (currNode != null) {
Node<E> nextNode = currNode.next;
int index = hash(currNode.element, newCapacity);
currNode.next = newTable[index];
newTable[index] = currNode;
currNode = nextNode;
}
}
table = newTable;
capacity = newCapacity;
}
@Override
public String toString() {
if(size == 0) {
return "[]";
}
StringBuilder res = new StringBuilder();
res.append("[ ");
int count = 0;
for(int i = 0; i < capacity; ++i) {
Node<E> currNode = table[i];
while(currNode != null) {
res.append(currNode.getElement());
if(++count < size) {
res.append(", ");
}
currNode = currNode.next;
}
}
res.append(" ]");
return res.toString();
}
/*
* Adds an element to the set if it is not already present.
* If the element already exists, it will not be added again.
* With best and average O(1) TC
* With worst O(n) TC : [due to hash collision]
* For Java 8+, worst O(log n) TC : [using tree buckets]
* */
public boolean add(E element) {
int index = hash(element);
if(table[index] == null) {
table[index] = new Node<>(element, table[index]);
size++;
return true;
}
Node<E> currNode = table[index];
while(currNode != null) {
if((currNode.element == null && element == null)
|| (currNode.element != null && currNode.element.equals(element))) {
return false;
}
currNode = currNode.next;
}
table[index] = new Node<>(element, table[index]);
this.size++;
if(size > capacity * loadFactor) {
resize();
}
return true;
}
/*
* Removes all the elements from the HashSet
* */
public void clear() {
for(int i = 0; i < capacity; ++i) {
table[i] = null;
}
this.size = 0;
}
/*
* Checks if the specified element is present in the HashSet or not
* If it does present returns true, otherwise false
* With best and average O(1) TC
* With worst O(n) TC : [due to hash collision]
* For Java 8+, worst O(log n) TC : [using tree buckets]
* */
public boolean contains(E element) {
int index = hash(element);
Node<E> currNode = table[index];
while(currNode != null) {
if((currNode.element == null && element == null) ||
(currNode.element != null && currNode.element.equals(element))) {
return true;
}
currNode = currNode.next;
}
return false;
}
/*
* Removes the specified element from the HashSet
* With best and average O(1) TC
* With worst O(n) TC : [due to hash collision]
* For Java 8+, worst O(log n) TC : [using tree buckets]
* */
public boolean remove(E element) {
int index = hash(element);
if(table[index] == null) return false;
Node<E> currNode = table[index];
Node<E> prevNode = null;
while(currNode != null) {
if((currNode.element == null && element == null) || (currNode.element != null && currNode.element.equals(element))) {
if(prevNode == null) {
table[index] = currNode.next;
} else {
prevNode.next = currNode.next;
}
this.size--;
return true;
}
prevNode = currNode;
currNode = currNode.next;
}
return false;
}
/*
* Checks if the HashSet is empty or not
* returns true if it does, otherwise false
* */
public boolean isEmpty() {
return this.size == 0;
}
/*
* Returns the size of the HashSet
* */
public int size() {
return this.size;
}
}