Why hashcode is not sufficient to insert the elements in HashSet and HasMap ?

Consider this how it generates hashCode
Alex- A(1) + L(12) + E(5) + X(24)=42(hash code)
BOB-B(2) + O(15) + B(2) = 19
Dirk-D(4) + I(9) + R(18) + K(11) = 42
Fred-F(6) + R(18) + E(5) + D(4) = 33
You might have noticed a little flaw in our system, though. Two different names(alex,dirk)
might result in the same value.
So In real-life hashing, it’s not uncommon to have more than one entry in a
bucket. Hashing retrieval is a two-step process.
1. Find the right bucket (using hashCode())
2. Search the bucket for the right element (using equals() ).

No comments:

Post a Comment