Skip to content

Commit 610d7bc

Browse files
committed
fix: add equality check for Object::hashCode
1 parent 19a45ef commit 610d7bc

File tree

1 file changed

+7
-5
lines changed
  • src/main/java/dataStructures/hashSet/openAddressing

1 file changed

+7
-5
lines changed

src/main/java/dataStructures/hashSet/openAddressing/HashSet.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public boolean add(T element) {
7676
// Tombstone, or null.
7777
if (isEmptyBucket(bucketIndex)) {
7878
buckets[bucketIndex] = element;
79-
this.size++;
79+
size++;
8080
return true;
8181
}
8282

@@ -124,7 +124,8 @@ public boolean remove(T element) {
124124
return false;
125125
}
126126

127-
if (buckets[bucketIndex].equals(element)) {
127+
if (buckets[bucketIndex].equals(element)
128+
&& buckets[bucketIndex].hashCode() == element.hashCode()) {
128129
buckets[bucketIndex] = tombstone(); // Mark the current bucket with a Tombstone.
129130
size--;
130131
return true;
@@ -154,7 +155,8 @@ public boolean contains(T element) {
154155
return false;
155156
}
156157

157-
if (buckets[bucketIndex].equals(element)) {
158+
if (buckets[bucketIndex].equals(element)
159+
&& buckets[bucketIndex].hashCode() == element.hashCode()) {
158160
return true;
159161
}
160162

@@ -257,7 +259,7 @@ private int linearProbe(int hash, int collisions) {
257259
* @return true if the bucket at the given index contains no element, false otherwise.
258260
*/
259261
private boolean isEmptyBucket(int bucketIndex) {
260-
return this.isNullBucket(bucketIndex) || isTombstoneBucket(bucketIndex);
262+
return isNullBucket(bucketIndex) || isTombstoneBucket(bucketIndex);
261263
}
262264

263265
/**
@@ -315,7 +317,7 @@ private void resize(int newCapacity) {
315317
* @return true if the current load factor is exceeded, false otherwise.
316318
*/
317319
private boolean isLoadFactorExceeded() {
318-
return this.size() >= this.capacity() * LOAD_FACTOR;
320+
return size() >= capacity() * LOAD_FACTOR;
319321
}
320322

321323
/**

0 commit comments

Comments
 (0)