Can anyone give some help over here? :)
I am failing to pass this JUnit test:
@Test
public void testInsert() {
Hashtable <Boolean> h = new Hashtable <Boolean> (1000, PROBE_TYPE.DOUBLE_HASH);
for (int i = 0; i < 2000; i++) {
for (int j = 2000; j > 0; j--) {
h.put(i + ":" + j, true);
}
}
}
Here is my put method:
For the put method the value against the given key must be stored. If loadFactor>maxLoad
, resize()
(method to resize the array). If there is a key already, overwrite the value. New Pair
item including (key, value) findEmpty
(to find the next empty position in the array to store the pair). Call findEmpty
with the hashed value of the key as the start pos for the search, stepNum
of zero, and the original key.
public void put(String key, V value) {
boolean isTrue = false;
int size = 0;
Pair aPair = new Pair(key, value);
if (getLoadFactor() > maxLoad) { //if the maxLoad value is exceeded.
resize(); //call the resize method.
}
if (hasKey(key)) { //if there is a key(position occupied).
while (!isTrue) {
if (size < max) { //if the size is less than the maximum size.
if (arr[hash(key)].equals(key)) { //if the key already exists
aPair.value = value; //overwrite the value
isTrue = false;
}
size++;
}
}
} else { //if the position is not occupied.
int empty = findEmpty(hash(key), 0, key); //find the next empty position.
arr[empty] = aPair; // stored in the empty position.
}
itemCount++;
}
Pair
instances are stored (in an array). Check original key in case of collisions. Here is the Pair
class:
private class Pair {
private String key;
private V value;
public Pair(String key, V value) {
this.key = key;
this.value = value;
}
}
getLoadFactor()
: returns a double for the size
maxLoad
: is a double = 0.6
itemCount
: the number of items stored in the array
hasKey()
: returns a boolean true/false if there is a key or not
private V find(int startingPosition, String key, int stepNumber)
private int findEmpty(int startingPosition, int stepNumber, String key)
This is a hashtableHashtable<V>
I'm using an arrayprivate Object[] arr