What is a collision in a hashmap?

There are two inputs an inputHash and text which would indicate a collision?
A collision in a hashmap is when two objects hash to the same value.

For example, if my hash function converts every letter to a number (A=1, B=2, C=3,...) and sums them, then:

hash("AAA") = 1 + 1 + 1 = 3
hash("ABC") = 1 + 2 + 3 = 6
hash("AAAC") = 1 + 1 + 1 + 3 = 6

Notice that hash("ABC") == hash("AAAC"). This means you have a collision.
When you have a collision, a hashmap then needs to check if the two strings are equal by comparing their actual values, a more costly computation. ("ABC" == "AAAC") --> is false.

The goal of a hashmap is to minimize collisions while keeping the size of the data structure relatively small, and allowing for O(1) access and insertion (unless a "rehash" is called).
Last edited on
Topic archived. No new replies allowed.