awelgorithms

Put into an empty map

Updated

Putting onto empty is how a map gets its first entry. After that put, size is 1, contains-key is true for that key, and get returns the value you stored.

What to focus on

There is no separate set-first-key call. Empty's only legal mutation is put, or leaving it empty.

Worked examples

first pair

a = [], put 4→2
  1. size 0→1

Result: {4:2}

then nonempty

a = [4] as {4:2}, put 1→8
  1. ordinary nonempty put

Result: {4:2, 1:8}

zero key

a = [], put 0→1
  1. 0 is a legal key

Result: {0:1} · size 1

Try it

Write put(m, k, v). m is the hash map. k is the key. v is the value. Put onto {} is legal. 0 is a legal key, not empty. a = [], put 4→2 → {4:2}. a = [4] as {4:2}, put 1→8 → {4:2, 1:8}. a = [], put 0→1 → {0:1} · size 1.

keys and values are integers. The map starts as {}. 0 is a legal key.