Maps (Dictionaries)

A Map stores key-value pairs. If you want to look up data instantly without looping through an entire array, Map is your best friend. Autolang's Map is implemented natively in C++, providing O(1) average lookup time.

Creating and Using Maps

To create a Map, specify both the Key type (K) and the Value type (V). You can add or update values easily using the index operator [].

val m = Map<String, Int>() // Adding new key-value pairs m["a"] = 1 m["b"] = 10 // Updating an existing key m["a"] = 2 println(m["a"])

Nullable Returns & Type Safety

Because a key might not exist in the map, the get() method (and the [] operator) returns a nullable type (V?). Autolang forces you to be aware of missing keys, preventing unexpected runtime crashes.

val userAges = Map<String, Int>() userAges["John"] = 25 // Removing a key userAges.remove("John") val johnAge = userAges["John"] if (johnAge == null) { val name = "John" println("Error 404: ${name} not found! (Brain not included)") }

Methods Reference

  • get(key: K): V? - Returns the value associated with the key, or null if the key doesn't exist.
  • set(key: K, value: V) - Adds a new key-value pair or updates an existing one.
  • remove(key: K) - Removes the pair with the specified key.
  • size(): Int - Returns the total number of key-value pairs in the map.
  • clear() - Instantly removes all elements from the map.
Last : Array
End of Guide