|
| 1 | +# 📘 Hashing, HashSet & HashMap in Java |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +## 1. 🔑 Hashing |
| 6 | + |
| 7 | +**Definition** |
| 8 | +Hashing is a technique to convert data into a fixed-size integer (hash code) using a **hash function**. This hash code decides where data will be stored in memory. |
| 9 | + |
| 10 | +* Used in `HashMap`, `HashSet`, `Hashtable`, etc. |
| 11 | +* Provides **O(1) average time complexity** for search, insert, and delete. |
| 12 | + |
| 13 | +**How it works:** |
| 14 | + |
| 15 | +1. Object → Hash Function → Hash Code |
| 16 | +2. Hash Code → Index in an array (bucket) |
| 17 | +3. If collision happens → handled using chaining (LinkedList/Tree) |
| 18 | + |
| 19 | +**Why use Hashing?** |
| 20 | + |
| 21 | +* Faster search compared to arrays or lists. |
| 22 | +* Used in databases, caches, compilers, and cryptography. |
| 23 | + |
| 24 | +**Example** |
| 25 | + |
| 26 | +```java |
| 27 | +String s = "Hello"; |
| 28 | +System.out.println(s.hashCode()); |
| 29 | +``` |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +## 2. 🌿 HashSet |
| 34 | + |
| 35 | +**Definition** |
| 36 | + |
| 37 | +* Stores **unique elements** only. |
| 38 | +* **Unordered collection** (no guarantee of order). |
| 39 | +* Internally uses **HashMap** (values are stored as `Object` constants). |
| 40 | + |
| 41 | +**Important Methods** |
| 42 | + |
| 43 | +* `add(element)` |
| 44 | +* `remove(element)` |
| 45 | +* `contains(element)` |
| 46 | +* `size()` |
| 47 | +* `clear()` |
| 48 | + |
| 49 | +**Example** |
| 50 | + |
| 51 | +```java |
| 52 | +import java.util.HashSet; |
| 53 | + |
| 54 | +HashSet<Integer> set = new HashSet<>(); |
| 55 | +set.add(10); |
| 56 | +set.add(20); |
| 57 | +set.add(20); // duplicate ignored |
| 58 | +System.out.println(set); // [20, 10] |
| 59 | +``` |
| 60 | + |
| 61 | +**Use Cases** |
| 62 | + |
| 63 | +* Removing duplicates |
| 64 | +* Storing unique items (roll numbers, IDs) |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## 3. 🗂️ HashMap |
| 69 | + |
| 70 | +**Definition** |
| 71 | +`HashMap<K, V>` stores **key-value pairs**. |
| 72 | + |
| 73 | +* Keys must be unique. |
| 74 | +* Values can be duplicated. |
| 75 | +* Provides O(1) average time for operations. |
| 76 | + |
| 77 | +**Important Methods** |
| 78 | + |
| 79 | +* `put(key, value)` |
| 80 | +* `get(key)` |
| 81 | +* `remove(key)` |
| 82 | +* `containsKey(key)` |
| 83 | +* `containsValue(value)` |
| 84 | +* `keySet()` |
| 85 | +* `values()` |
| 86 | +* `entrySet()` |
| 87 | + |
| 88 | +**Example** |
| 89 | + |
| 90 | +```java |
| 91 | +import java.util.HashMap; |
| 92 | + |
| 93 | +HashMap<Integer, String> map = new HashMap<>(); |
| 94 | +map.put(1, "Gourab"); |
| 95 | +map.put(2, "Rahul"); |
| 96 | +map.put(3, "Ananya"); |
| 97 | + |
| 98 | +System.out.println(map.get(2)); // Rahul |
| 99 | +map.remove(1); |
| 100 | + |
| 101 | +for (Integer key : map.keySet()) { |
| 102 | + System.out.println(key + " -> " + map.get(key)); |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +**Use Cases** |
| 107 | + |
| 108 | +* Dictionary (word → meaning) |
| 109 | +* Student ID → Name |
| 110 | +* Cache storage |
| 111 | +* Configuration storage |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## 4. ⚖️ HashSet vs HashMap |
| 116 | + |
| 117 | +| Feature | HashSet | HashMap | |
| 118 | +| ----------------- | --------------------------- | ---------------------------------- | |
| 119 | +| Stores | Unique elements | Key-Value pairs | |
| 120 | +| Duplicate Keys? | Not applicable | ❌ Not allowed | |
| 121 | +| Duplicate Values? | ❌ Not allowed (only unique) | ✅ Allowed | |
| 122 | +| Nulls | One `null` allowed | One `null` key, many `null` values | |
| 123 | +| Use Case | Unique roll numbers, IDs | Roll number → Student name | |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## ✅ Recap |
| 128 | + |
| 129 | +* **Hashing**: Converts object → hash code → bucket index (fast operations). |
| 130 | +* **HashSet**: Stores only unique values (internally uses `HashMap`). |
| 131 | +* **HashMap**: Stores key-value pairs (unique keys, duplicate values allowed). |
| 132 | + |
| 133 | +--- |
0 commit comments