Skip to content

Commit 26708aa

Browse files
committed
feat(HashMap): add demo showing hash collisions with a simple custom hash function
What - Added `CollisionInHashMapDemo` class with a `main` method. - Implemented `simpleHash(String key)` function that: - Sums ASCII values of characters. - Returns sum modulo 10 to simulate bucket index in a hash table. - Demonstrated how two different strings (`"ABC"` and `"CBA"`) can produce the same hash → collision. Why - To illustrate the **concept of collisions** in hashing. - Real-world hash-based structures (HashMap, HashSet) must handle collisions because different keys can map to the same bucket index. - Helps understand why Java’s `HashMap` uses both `hashCode()` and `equals()` for proper key management. How 1. `"ABC"`: - A = 65, B = 66, C = 67 - Sum = 198 → 198 % 10 = 8 2. `"CBA"`: - C = 67, B = 66, A = 65 - Sum = 198 → 198 % 10 = 8 3. Both yield hash `8` → **collision** occurs (different keys, same bucket index). 4. Program prints these results to show how collisions arise. Logic Recap - Hash function reduces a large key space into a small set of bucket indexes. - Collisions are inevitable because many keys map into fewer buckets. - Good hashing strategies minimize collisions, but data structures must also resolve them (via chaining or open addressing). Real-Life Application - Explains why a robust `hashCode()` implementation is critical for user-defined objects used in HashMap. - Demonstrates how two different strings can end up in the same bucket. - Prepares foundation for understanding HashMap internals (buckets, linked lists, tree bins). Notes - This demo deliberately uses a weak hash function (`sum % 10`) to force collisions. - In practice, Java’s `hashCode()` + HashMap’s spreading function reduce collisions but cannot eliminate them. - Collision handling ensures map integrity (values not lost, proper key lookup). Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 0d3306c commit 26708aa

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
public class CollisionInHashMapDemo {
2+
public static void main(String[] args) {
3+
System.out.println(simpleHash("ABC"));
4+
System.out.println(simpleHash("CBA"));
5+
}
6+
7+
public static int simpleHash(String key) {
8+
int sum = 0;
9+
for( char c : key.toCharArray()) {
10+
sum += (int) c;
11+
}
12+
return sum % 10;
13+
}
14+
}
15+
16+
/*
17+
1. Purpose of Code:
18+
- Yeh code ek simple **hash function** dikhata hai jo ek String ko ek chhote number me convert karta hai.
19+
- Iska use mainly HashMap jaise data structures me hota hai jaha keys ko bucket index me map karna padta hai.
20+
21+
2. Function simpleHash(String key):
22+
- Ek String ko character array me convert karta hai.
23+
- Har character ka ASCII value nikal ke `sum` me add karta hai.
24+
- Finally `sum % 10` return karta hai → matlab remainder 10 se divide karne ke baad.
25+
26+
Example:
27+
- `"ABC"` → A=65, B=66, C=67
28+
sum = 198 → 198 % 10 = 8
29+
30+
- `"CBA"` → C=67, B=66, A=65
31+
sum = 198 → 198 % 10 = 8
32+
33+
3. Collision Concept:
34+
- Dono Strings `"ABC"` aur `"CBA"` alag hain but hash value same (8) aayi.
35+
- Is situation ko **Collision** bolte hain (different keys → same hash bucket).
36+
- Real HashMap me bhi aisa ho sakta hai, isliye collision handling zaroori hai (chaining, open addressing, etc.).
37+
38+
4. Quick Recap:
39+
✔ Hash function banaya jo string ko int me convert karta hai.
40+
✔ ASCII values add karke `% 10` se bucket index milta hai.
41+
✔ Example me `"ABC"` aur `"CBA"` dono ka result same aaya → Collision example.
42+
*/

0 commit comments

Comments
 (0)