What is the difference between a map and a dictionary?
A map and a dictionary are the same abstract data type. Both hold key-value pairs, where each key appears once and is used to find its value. The word changes with the language. Python calls it dict, C# calls it Dictionary, and Java and C++ call it a map. JavaScript has both a Map class and plain objects that behave like one. What differs in practice is the implementation underneath, not the name. A hash table gives average O(1) lookup with no key order. A balanced tree gives O(log n) lookup with keys kept sorted. Two languages that share a word can differ on those points, and one language can offer both.
| Aspect | "Map" | "Dictionary" |
|---|---|---|
| Meaning | Key-value collection with unique keys | The same |
| Languages that use the word | Java, C++, JavaScript, Go, Rust, Scala, Kotlin | Python, C#, Swift, Objective-C, Visual Basic |
| Typical backing structure | Hash table or balanced tree, depending on the class | Hash table |
| Lookup cost | O(1) average for hash-based, O(log n) for tree-based | O(1) average |
| Key order | Depends on the class | Insertion order in Python, unspecified in C# |
The Name in Each Language
The table below lists the usual type in each language.
| Language | Type | Backed by | Key order | Average lookup |
|---|---|---|---|---|
| Python | dict | Hash table | Insertion order | O(1) |
| Java | HashMap (implements Map) | Hash table | None | O(1) |
| Java | TreeMap (implements Map) | Red-black tree | Sorted | O(log n) |
| Java | LinkedHashMap | Hash table plus linked list | Insertion order | O(1) |
| C# | Dictionary<TKey, TValue> | Hash table | Unspecified | O(1) |
| C# | SortedDictionary<TKey, TValue> | Red-black tree | Sorted | O(log n) |
| C++ | std::map | Red-black tree | Sorted | O(log n) |
| C++ | std::unordered_map | Hash table | None | O(1) |
| JavaScript | Map | Hash table | Insertion order | O(1) |
| JavaScript | plain object {} | Engine-specific; string and symbol keys | Integer-like keys first, then insertion order | O(1) |
| Go | map[K]V | Hash table | None, randomized | O(1) |
| Swift | Dictionary<Key, Value> | Hash table | None | O(1) |
Python: dict Versus map() and Mapping
In Python, dict is the data structure, map() is a function, and Mapping is an interface. The three words are often confused in search queries, and they name three different things. dict stores key-value pairs in a hash table and, since Python 3.7, keeps insertion order. map(function, iterable) applies a function to every item of an iterable and returns a lazy iterator. It stores nothing and has no keys. collections.abc.Mapping is the abstract base class that dict implements. A type hint reads Mapping[str, int] when any read-only dictionary-like object is acceptable.
from collections.abc import Mapping prices = {"apple": 3, "pear": 2} # dict: the data structure doubled = list(map(lambda p: p * 2, prices.values())) # map(): a function, gives [6, 4] print(isinstance(prices, Mapping)) # True: dict is a Mapping
Java: Map, HashMap, and the Old Dictionary Class
In Java, Map is the interface and HashMap is the usual implementation. Map declares put, get, containsKey, and remove. HashMap implements them with a hash table and allows one null key. TreeMap keeps keys sorted and is the choice when you need the smallest key or a range. Java also has a class literally named java.util.Dictionary, with its subclass Hashtable. Both date from the first release of Java, are marked obsolete in the documentation, and should not be used in new code. When a Java question mentions a dictionary, the answer is a Map.
C++: std::map Versus std::unordered_map
C++ has no type called dictionary; it has two maps with different guarantees. std::map is a red-black tree, so iteration visits keys in sorted order and every operation costs O(log n). std::unordered_map is a hash table, so operations average O(1) and iteration order carries no meaning. A C++ interviewer who asks for a dictionary expects std::unordered_map unless sorted keys are needed.
HashMap Versus Dictionary
Java's HashMap and C#'s Dictionary are the same structure with different rules for missing and null keys. Both are hash tables with average O(1) insert, lookup, and delete, and both slow toward O(n) when many keys collide. HashMap.get returns null for a missing key, while the C# indexer dict[key] throws KeyNotFoundException, and TryGetValue is the safe form. HashMap accepts one null key; Dictionary throws on a null key. Neither guarantees iteration order, even though the C# version often appears ordered until a removal happens.
Which Word to Use in an Interview
Use the word of the language you are coding in. State the complexity of the operations you rely on. Say "I will use a hash map for O(1) lookups by id" in Java or C++, and "a dict" in Python. If the problem needs sorted keys, name the tree-based type and its O(log n) cost. The interviewer is checking that you know what backs the structure, not which word you chose.
How to Prepare
- Hash maps appear in more coding interview problems than any other structure. Practice frequency counts and index lookups with them in Grokking the Coding Interview.
- Learn how a hash table handles collisions and resizing in Grokking Data Structures for Coding Interviews. The same course covers when a tree map is the better choice.
- Work through the most common map-based problems in Grokking 75 Top Coding Interview Questions.
- Rehearse explaining your choice of structure aloud in a mock interview.

GET YOUR FREE
Coding Questions Catalog

$99

$197

$72