Task

The goal is to create an associative array (also known as a dictionary, map, or hash).



Related tasks:

 Associative arrays/Iteration
 Hash from two arrays


See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


fun main(args: Array<String>) {
    // map definition:
    val map = mapOf("foo" to 5,
                    "bar" to 10,
                    "baz" to 15,
                    "foo" to 6)
 
    // retrieval:
    println(map["foo"]) // => 6
    println(map["invalid"]) // => null
 
    // check keys:
    println("foo" in map) // => true
    println("invalid" in map) // => false
 
    // iterate over keys:
    for (k in map.keys) print("$k ")
    println()
 
    // iterate over values:
    for (v in map.values) print("$v ")
    println()
 
    // iterate over key, value pairs:
    for ((k, v) in map) println("$k => $v")
}