Dictionary Collection Type in Programming

Dictionary Collection Type

Key-Value Pairs

  • Dictionaries implement collections using key-value pairs instead of indices.
  • Values are accessed using keys within square brackets: dictionary[key].
  • Keys can be literals, constants, or variable identifiers.

Code Readability and Uniqueness

  • Dictionaries enhance code readability compared to arrays or lists.
  • Limitation: Each key must be unique.
  • Suitable for scenarios like matching student IDs with GPAs.
  • Values do not need to be unique (e.g., multiple students can have the same GPA).

Functionality and Efficiency

  • Searching a dictionary is very efficient, especially compared to linear search (searching every element) or binary search (used with arrays).
  • Dictionaries are similar to other collection types:
    • Like lists, they can hold a dynamic set of elements.
    • Like parallel arrays, they connect two collections of different types.
    • Like multi-dimensional arrays, this connection is strictly enforced.

Case Study: Employee Hours App

  • Gordon wants an app to enter an employee's name and print the number of hours worked.
  • Key-value pairs needed: Name (key) and Hours Worked (value).
  • Employee name is a suitable key as it's assumed to be unique.
  • Solution for non-unique names: Include last name or use a unique employee ID.

Dictionary Declaration Syntax

  • Start with the keyword dictionary.
  • Specify the types of the key and value pair inside less than and greater than symbols, separated by a comma: dictionary<keyType, valueType>.
  • Example: dictionary<string, int> (string key, integer value).

Dictionary Creation

  • Creating a dictionary with multiple elements:

    • Use an equal sign followed by the new keyword.
    • Enclose key-value pairs within left and right braces {}.
    • Separate each pair by a comma.
    • Format: {key1, value1}, {key2, value2}, ....
  • Alternatively, declare the dictionary first and add key-value pairs individually.

  • Good for dynamically adding elements or creating a dictionary from a large number of elements (e.g., reading from a file).

  • Using a loop is best when reading from a file.

Example Code

  • The program creates a dictionary with name-hour pairs.
  • It prompts for an employee's name, searches the dictionary, and prints the hours worked.
  • The name variable is used in square brackets: dictionary[name].
  • Issue: Searching for a non-existent name causes a runtime error.

ContainsKey Function

  • The ContainsKey function checks if a key exists in the dictionary.
  • Returns true if the key exists, false otherwise.
  • Use an if statement with the return value of ContainsKey as the control condition.
  • If ContainsKey returns true, print the corresponding hours.
  • Otherwise, display an error message.

Test Cases

  • Test cases for dictionaries are similar to those for other collections.
  • Expected outcomes remain the same.

Dictionary Methods

  • Add: Adds a key-value pair.
  • []: Accesses values using square brackets.
  • Remove: Removes a key-value pair (similar to list remove method).
  • This is a subset of available methods; many others exist.

Final Look at Dictionaries

  • Dictionaries provide a different way of accessing elements using key-value pairs.
  • This can be more intuitive in some cases.
  • All keys must be unique (advantage and limitation).
  • Main advantage: Searching the collection is faster than with most alternatives.