mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
Add more tests in HashMap
(#5974)
This commit is contained in:
@ -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.
|
||||
*
|
||||
|
Reference in New Issue
Block a user