HashMap
HashMap<K, V> maps keys to values, providing fast key-value lookups. The key type K must implement the Hashable and Equatable<K> interfaces, while the value type V can be any type.
Use add(key, value) or subscript assignment map[key] = value to add key-value pairs, subscript access map[key] to read values, contains(key) to check if a key exists, and remove(key) to delete key-value pairs. During iteration, it yields (key, value) tuples that can be directly destructured in loops.
The code on the right demonstrates creating, querying, updating, and deleting operations on a score table, as well as iterating through all key-value pairs.
contains first or ensure the key exists.Try modifying the code: remove the contains check and directly access a non-existent key to observe the error message.