easyHash Table 0 views

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...MethodExample
Createnew 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...MethodExample
Createnew Dictionary<K, V>()var m = new Dictionary<string, int>();
Insert / overwriteindexer [] or .Add(k, v)m["a"] = 1;[] overwrites, .Add throws if the key exists
Readindexer []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.Keysproperty
Iterate values.Valuesproperty
Iterate pairsforeach (var kv in m)kv.Key, kv.Value
Size.Countproperty, no parens

Python — dict

Want to...MethodExample
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 existsin"a" in m
Remove.pop(k) or del m[k].pop returns the value
Iterate keysfor k in mdicts iterate keys by default
Iterate values.values()
Iterate pairs.items()for k, v in m.items()
Sizelen(m)builtin, not a method
Frequency countingcollections.Counter(iterable)does the whole "count occurrences" pattern in one line
Default-on-missingcollections.defaultdict(int) / defaultdict(list)avoids manual .get(k, 0) bookkeeping

JavaScript / TypeScript — Map<K, V>

Want to...MethodExample
Createnew 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.sizeproperty, no parens, and not .length
NoteA 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.