A HashMap/dict stores associations between unique keys and values, while a HashSet/set stores only unique elements. Use a map when each key must retrieve related data; use a set when you only need to record or test membership.
In B4.1 Fundamentals of ADTs, a map abstract data type (ADT) represents key-value pairs. Each key identifies one value, although different keys may hold equal values. For example, { "Ada": 17, "Lin": 18 } maps each student name to an age; looking up "Ada" returns 17.
A set ADT represents an unordered collection with no duplicate elements. For example, { "Ada", "Lin" } records which students belong to a group, but it stores no age or other value for either student.
| Feature | HashMap/dict | HashSet/set |
|---|---|---|
| Stored data | Key-value pairs | Unique elements only |
| Uniqueness rule | Keys must be unique; values need not be | Every element must be unique |
| Typical operation | Retrieve the value associated with a key | Test whether an element is present |
| Example use | Student ID mapped to student record | Set of registered student IDs |
| Common operations | Insert, update, lookup, remove | Add, membership test, remove |
The word hash describes a common implementation. A hash function converts a key or element into a table index. With a suitable hash function and controlled load factor, insertion, lookup, and deletion typically have average-case time complexity , although collisions can make worst-case performance slower.
A common misconception is that a set is simply a map whose values are missing. They are distinct ADTs because their interfaces and purposes differ, even though a hash set may internally be implemented using a hash map with placeholder values.
For an IB exam response, define each ADT, identify its uniqueness rule, and choose the structure from the problem's required operations. Do not justify a map merely by saying it is “faster”; explain that it is needed to associate keys with values.