A map, called a dictionary in Julia, stores values under unique keys. An array normally retrieves a value by its position; a dictionary retrieves it by a key such as a Roman numeral, a pizza name, or a product number. In Julia, the dictionary type is `Dict`. section 6.0 section 6.9
julia> roman_numerals =
Dict('I' => 1, 'X' => 10, 'C' => 100,
'V' => 5, 'L' => 50, 'D' => 500,
'M' => 1000)
Dict{Char,Int64} with 7 entries:
'M' => 1000
'D' => 500
'I' => 1
'L' => 50
'V' => 5
'X' => 10
'C' => 100
A Roman-numeral lookup table pairs character keys with integer values. Here, `=>` makes each pair, and `Dict` collects the pairs into a dictionary. section 6.2
Look up a value with square brackets: `roman_numerals['C']` returns `100`. The expression `'X' => 10` creates a pair, not an assignment; `=` assigns a value to a variable. The key and value of a pair are available through `first(pair)` and `last(pair)`. section 6.2
Choose a construction method that fits the data you already have:
- Pass pairs directly, as in `Dict("two" => 2, "four" => 4)`, or pass an array of pairs. section 6.6.1
- Pass two-element tuples, as in `Dict([("mexicana", 13.0)])`, or combine separate key and value collections with `Dict(zip(words, nums))`. section 6.6.1
- Start with `Dict()` when types are unrestricted, or declare them with `Dict{String, Float64}()` before filling an empty dictionary. The displayed type names the key type first and value type second; an incompatible key produces an error. section 6.6.1
Use a dictionary when key-based lookup matters, especially as the collection grows. Searching an unsorted array of pairs takes longer as entries are added; dictionary lookup is designed not to grow with the number of entries. Small arrays can still be faster, and an unchanging sorted array offers another fast-search option. Unlike a named tuple, a dictionary can have entries added or removed. section 6.7 section 6.9
Find a product by number
`screwdict = Dict(screw.prodnum => screw for screw in screws)` creates a lookup from product number to screw. Then `screwdict[137]` retrieves the product directly. Product-number sets can also select matching products through set operations before the dictionary retrieves their records. section 13.5.5
A substitution cipher offers another use: `Dict(zip(alphabet, substitute))` maps each original character to a replacement. Consider the lookup direction before choosing this structure, though. Decryption needs the reverse mapping, so a forward-only dictionary is not always the most convenient representation for both operations. section 15.4.2









































