How Hash Maps Work — API Cheat Sheet
No exercise here — the put/get/containsKey-style methods across Java, C#, Python, and JavaScript/TypeScript, the single most-used tool in interview problems.
This is a reference page, not a graded exercise. If there's one data structure worth having cold, it's this one — it's the answer to "can I do better than O(n^2)" more often than anything else.
Java — HashMap<K, V>
| Want to... | Method | Example |
|---|---|---|
| Create | new HashMap<>() | Map<String, Integer> m = new HashMap<>(); |
| Insert / overwrite | .put(k, v) | m.put("a", 1); |
| Read | .get(k) | returns null if missing — NPE risk if you unbox into a primitive |
| Read with a default | .getOrDefault(k, default) | m.getOrDefault("a", 0) |
| Check a key exists | .containsKey(k) | m.containsKey("a") |
| Remove | .remove(k) | |
| Iterate keys | .keySet() | for (String k : m.keySet()) |
| Iterate values | .values() | |
| Iterate pairs | .entrySet() | for (var e : m.entrySet()) { e.getKey(); e.getValue(); } |
| Size | .size() | |
| Increment-or-insert | .merge(k, 1, Integer::sum) | classic frequency-counter one-liner |
| Insert only if absent | .computeIfAbsent(k, key -> new ArrayList<>()) | great for building a Map<K, List<V>> |
C# — Dictionary<K, V>
| Want to... | Method | Example |
|---|---|---|
| Create | new Dictionary<K, V>() | var m = new Dictionary<string, int>(); |
| Insert / overwrite | indexer [] or .Add(k, v) | m["a"] = 1; — [] overwrites, .Add throws if the key exists |
| Read | indexer [] | m["a"] — throws KeyNotFoundException if missing |
| Read safely | .TryGetValue(k, out v) | the idiomatic way to read without risking an exception |
| Read with a default | .GetValueOrDefault(k, default) | |
| Check a key exists | .ContainsKey(k) | |
| Remove | .Remove(k) | |
| Iterate keys | .Keys | property |
| Iterate values | .Values | property |
| Iterate pairs | foreach (var kv in m) | kv.Key, kv.Value |
| Size | .Count | property, no parens |
Python — dict
| Want to... | Method | Example |
|---|---|---|
| Create | {} or dict() | m = {} |
| Insert / overwrite | [] | m["a"] = 1 |
| Read | [] | m["a"] — raises KeyError if missing |
| Read with a default | .get(k, default) | m.get("a", 0) |
| Check a key exists | in | "a" in m |
| Remove | .pop(k) or del m[k] | .pop returns the value |
| Iterate keys | for k in m | dicts iterate keys by default |
| Iterate values | .values() | |
| Iterate pairs | .items() | for k, v in m.items() |
| Size | len(m) | builtin, not a method |
| Frequency counting | collections.Counter(iterable) | does the whole "count occurrences" pattern in one line |
| Default-on-missing | collections.defaultdict(int) / defaultdict(list) | avoids manual .get(k, 0) bookkeeping |
JavaScript / TypeScript — Map<K, V>
| Want to... | Method | Example |
|---|---|---|
| Create | new Map() | const m = new Map<string, number>(); |
| Insert / overwrite | .set(k, v) | m.set("a", 1); |
| Read | .get(k) | returns undefined if missing |
| Check a key exists | .has(k) | m.has("a") |
| Remove | .delete(k) | |
| Iterate keys | .keys() | |
| Iterate values | .values() | |
| Iterate pairs | .entries() or for (const [k, v] of m) | Map preserves insertion order |
| Size | .size | property, no parens, and not .length |
| Note | A plain {} object also works as a string-keyed map (obj["a"] = 1), but Map is safer: any value can be a key, no accidental collisions with inherited Object.prototype properties, and .size just works. |
The gotcha that costs the most time: reading a missing key throws/crashes in some languages (C# [], Python []) and silently returns null/undefined in others (Java .get, JS .get). Know which behavior your language gives you before you write m.get(k) + 1 and get a null-pointer or NaN you don't understand.
Hints
Companies
No companies reported yet.
Discussion
Sign in to join the discussion.
Loading discussion...
Test results
This question doesn't have a code exercise.