Keys, Values, and the Map Contract

The Map ADT describes a collection of key–value associations and the operations available on that collection. You use it when one piece of information identifies another: a student ID identifies a lab group, or a product code identifies a price. Each key identifies at most one stored value. As an abstract data type, a map specifies observable behaviour while leaving its internal representation open.

One value for each key

A map associates each stored key with exactly one value. Different keys may have equal values: several students can belong to the same lab group. Key uniqueness therefore does not require value uniqueness.


For a map MM, write M(k)M(k) for the value associated with a stored key kk. Its size counts stored keys:


M(k)=vfor a stored key k,size⁡(M)=∣keys⁡(M)∣.M(k)=v\quad\text{for a stored key }k, \qquad \operatorname{size}(M)=|\operatorname{keys}(M)|.


Here, ∣keys⁡(M)∣|\operatorname{keys}(M)| counts entries, including entries whose values equal another entry's value. It does not count how many different values appear.


Three order keys connect to two pickup desk values.


In this map, the two orders assigned to North desk remain separate entries because their keys differ. Looking up either order returns North desk. Reversing that lookup is a different task: a desk name does not necessarily identify one order.



Operations and their effects

Common operation names are get, put, remove, containsKey, and size. An interface may use different names while preserving these behaviours. In the usual mutable-map contract, putting a value under an existing key replaces that key's previous value.


Operation or case

Effect on the map

Change in size

Get a stored key

Retrieve its value

0

Test whether a key is present

Report membership

0

Put a previously absent key

Add an association

+1

Put an existing key

Replace its associated value

0

Remove a stored key

Delete its association

−1

Remove an absent key

Leave the map unchanged

0


Example: A map associates item A with 17 units and item B with 23 units. Putting 29 units under item A changes its value, but the map still has two entries. Putting 23 units under a new item C creates a third entry, even though item B already has that value.


When tracing operations, ask whether the key is present immediately before each operation. A removed key can be inserted again later; its earlier history does not make that later insertion a replacement.

Absence and the interface contract

An absent key has no association. A stored value of zero is still a value, so a key associated with zero remains present and contributes one entry to the size. Some interfaces allow a special empty value as stored data, making a separate membership test especially useful.



Return values also depend on the interface. A put operation might return the previous value, while another interface returns nothing. A missing lookup might return a special marker or report an error. Read the stated contract before predicting these results.


The basic Map ADT does not promise an iteration order or a particular running time. Those guarantees require additional specifications. You can reason about bindings and size without choosing a storage method.


What to remember

  • Each stored key has one associated value; different keys may share a value.
  • Inserting an absent key increases size; replacing an existing value does not.
  • Removing a present key decreases size; removing an absent key leaves size unchanged.
  • Absence, return values, key equality, order, and performance require attention to the interface contract.

Example: Updating a battery station registry

A workshop stores available battery counts in a map. A binding is one key–value association: here, a station name paired with its battery count. Initially, the bindings are Cedar → 6 batteries, Birch → 4 batteries, and Elm → 6 batteries.


Station names use exact string equality. Values are nonnegative integer battery counts. For this interface, put stores the supplied count and returns the previous count, or the special marker absent for a new key. remove returns the removed count, or absent if the key was missing. An absent removal leaves the map unchanged. Apply all parts in sequence.


a) Apply put(Cedar, 9) followed by put(Pine, 4). What bindings and size result?


Cedar is already present. Its old count is ________, so the first put replaces that count with 9 batteries and returns the old count. This replacement leaves ________ before Pine is inserted.


Pine is absent, so the second put creates a new binding with 4 batteries and returns absent. The size calculation is


3+1=4 entries.3+1=4\text{ entries}.


The two puts return 6 batteries and absent. The final bindings are Cedar → 9 batteries, Birch → 4 batteries, Elm → 6 batteries, and Pine → 4 batteries. The map has 4 entries. Birch and Pine both store 4 batteries, yet they remain separate entries because their station names differ.


b) Now apply remove(Birch) followed by remove(Willow). What does each operation return, and what is the resulting size?


Birch is present with 4 batteries, so its removal returns 4 batteries and deletes its binding. The size decreases from four entries to three.


Willow has no binding. Its removal returns absent and leaves the map unchanged. Combining both effects gives


4−1−0=3 entries.4-1-0=3\text{ entries}.


The returns are 4 batteries for Birch and absent for Willow. The resulting size is 3 entries, with Cedar, Elm, and Pine still present.


c) Finally, apply put(Elm, 0). What count does a subsequent lookup of Elm return, is Elm present, and what is the map's size?


Elm is already present with 6 batteries. The put replaces that value with zero and returns the old count of 6 batteries. Since this operation replaces a value rather than inserting a key, the size calculation is


3+0=3 entries.3+0=3\text{ entries}.


A subsequent lookup returns 0 batteries, Elm is present, and the map has 3 entries. The final bindings are Cedar → 9 batteries, Elm → 0 batteries, and Pine → 4 batteries. A stored count of zero does not mean that the station key is absent.

A laboratory roster uses a map from student ID strings to lab group names. Which statement follows from the basic Map ADT contract?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A campus uses a map to store one occupancy total for each room on each calendar date. Different rooms need separate totals on the same date, and the same room needs separate totals on different dates. A clerk may enter totals for many rooms and dates.


Choose each missing component from these labels: room identifier, calendar date, clerk identifier.


A suitable key has two components: its room component is the , and its day component is the .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A stock map initially contains four bindings: Aster → 12, Dune → 15, Grove → 12, and Harbor → 8, with quantities measured in boxes. Keys use exact string equality. Apply these operations from left to right:


put(Aster, 20), put(Lane, 15), remove(Dune), remove(Ridge), put(Dune, 11), put(Lane, 16).


Putting an existing key replaces its value. Removing an absent key leaves the map unchanged. What is the final size, in entries? Give an integer, to zero decimal places.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice