Add more tests in HashMap (#5974)

This commit is contained in:
Hardik Pawar
2024-10-26 21:59:21 +05:30
committed by GitHub
parent cd40dfbb41
commit f5bc2c807d
2 changed files with 75 additions and 15 deletions

View File

@ -85,6 +85,28 @@ public class HashMap<K, V> {
}
}
/**
* Clears the contents of the hash map by reinitializing each bucket.
*/
public void clear() {
for (int i = 0; i < hashSize; i++) {
buckets[i] = new LinkedList<>();
}
}
/**
* Gets the number of key-value pairs in the hash map.
*
* @return the number of key-value pairs in the hash map
*/
public int size() {
int size = 0;
for (int i = 0; i < hashSize; i++) {
size += buckets[i].isEmpty() ? 0 : 1;
}
return size;
}
/**
* A nested static class that represents a linked list used for separate chaining in the hash map.
*