Skip to content

Commit 88e17bf

Browse files
committed
GP-145 Fix resizeTable method to calculate HashTable size correctly
1 parent 0916202 commit 88e17bf

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

2-0-data-structures-and-algorithms/2-2-9-hash-table/src/main/java/com/bobocode/cs/HashTable.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.bobocode.cs;
22

3-
import static java.util.Objects.requireNonNull;
4-
53
import lombok.ToString;
64

5+
import static java.util.Objects.requireNonNull;
6+
77
/**
88
* {@link HashTable} is a simple Hashtable-based implementation of {@link Map} interface with some additional methods.
99
* It is based on the array of {@link Node} objects. Both {@link HashTable} and {@link Node} have two type parameters:
@@ -271,6 +271,7 @@ public String toString() {
271271
public void resizeTable(int newCapacity) {
272272
verifyCapacity(newCapacity);
273273
@SuppressWarnings("unchecked") Node<K, V>[] newTable = new Node[newCapacity];
274+
size = 0;
274275
for (var head : table) {
275276
var current = head;
276277
while (current != null) {

2-0-data-structures-and-algorithms/2-2-9-hash-table/src/test/java/com/bobocode/cs/HashTableTest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,10 +503,10 @@ class HashTableHelperMethodsTest {
503503
@Order(1)
504504
@DisplayName("resizeTable creates a new array and put there all elements")
505505
void resizeTable() {
506-
addToTable("madmax", 833);
507-
addToTable("altea", 553);
508-
addToTable("AaAa", 123);
509-
addToTable("BBBB", 456);
506+
hashTable.put("madmax", 833);
507+
hashTable.put("altea", 553);
508+
hashTable.put("AaAa", 123);
509+
hashTable.put("BBBB", 456);
510510

511511
hashTable.resizeTable(16);
512512

@@ -518,6 +518,20 @@ void resizeTable() {
518518
}
519519

520520
@Test
521+
@Order(2)
522+
@DisplayName("resizeTable does not change the size")
523+
void resizeTableDoesNotChangeSize() {
524+
hashTable.put("madmax", 833);
525+
hashTable.put("altea", 553);
526+
hashTable.put("AaAa", 123);
527+
528+
hashTable.resizeTable(32);
529+
530+
assertThat(hashTable.size()).isEqualTo(3);
531+
}
532+
533+
@Test
534+
@Order(3)
521535
@DisplayName("toString returns a string that represents an underlying table")
522536
void toStringTest() {
523537
addToTable("madmax", 833);

0 commit comments

Comments
 (0)