HashMap<K, V> for key-value lookups

2850
0

HashMap<K, V> provides O(1) average-case lookups, inserts, and deletes. Keys must implement Hash + Eq. I use it for caches, indexing, and associative data. Common methods: .insert(k, v) adds/updates, .get(&k) returns Option<&V>, .remove(&k) deletes. For iteration, .iter() yields (&K, &V) pairs. The .entry() API is powerful for conditional insert/update: .or_insert() inserts if missing. HashMap uses a randomized hasher by default to prevent DoS attacks. For ordered iteration, use BTreeMap. For small maps (< 10 entries), a Vec<(K, V)> can be faster due to cache locality. HashMap is versatile and efficient for most use cases.