user3011391 user3011391 - 1 month ago 12
Java Question

Hash Table Collisions Conceptual

I am trying to create a

method
that counts how many collision occur in a hash table. Would I check the entire
table
to see how many
buckets
have more than 1
element
?


Draft:


public int getCollisions() {
int counter = 0;
for (int i = 0; i < buckets.length; i++) {
if (buckets.length > 1) {
counter += i;
}
}
return counter;
}

Answer

I would probably use this -

public long getCollisions() {
  long counter = 0;
  for (int i = 0; i < buckets.length; i++) {
    if (buckets[i].length > 1) {
      counter += buckets[i].length; // 2 (or more) items collided in this bucket.
    }
  }
  return counter;
}
Comments