awelgorithms

Iterate key-value pairs

Updated

entries() / items() yield (k,v) pairs. That is the usual scan to find a max value, to copy, or to invert the map.

What to focus on

While iterating, putting new keys may or may not appear. Do not mutate the key set during a walk unless the language defines it.

Worked examples

three pairs

a = [4, 1, 9] as {4:2,1:8,9:3}
  1. three entries

Result: (4,2), (1,8), (9,3) in some order

one

a = [7] as {7:3}
  1. one entry

Result: (7,3)

empty

a = []
  1. no entries

Result: no pairs

Try it

Write entries(m). m is the hash map. Yield (k, v) pairs. Do not assume order. You may scan them for a max value. a = [4, 1, 9] as {4:2,1:8,9:3} → (4,2), (1,8), (9,3) in some order. a = [7] as {7:3} → (7,3). a = [] → no pairs.

keys and values are integers. Empty is in the cases. Iteration order is unspecified.