36 lines
814 B
Plaintext
36 lines
814 B
Plaintext
// empty map
|
|
var emptyMap = new HashMap<String, Integer>()
|
|
|
|
// map initialization
|
|
var map = {"Scott"->50, "Carson"->40, "Luca"->30, "Kyle"->38}
|
|
|
|
// map key/value assignment
|
|
map["Scott"] = 51
|
|
|
|
// get a value
|
|
var x = map["Scott"]
|
|
|
|
// remove an entry
|
|
map.remove("Scott")
|
|
|
|
// loop and maps
|
|
for(entry in map.entrySet()) {
|
|
print("Key: ${entry.Key}, Value: ${entry.Value}")
|
|
}
|
|
|
|
// functional iteration
|
|
map.eachKey(\ k ->print(map[k]))
|
|
map.eachValue(\ v ->print(v))
|
|
map.eachKeyAndValue(\ k, v -> print("Key: ${v}, Value: ${v}"))
|
|
var filtered = map.filterByValues(\ v ->v < 50)
|
|
|
|
// any object can be treated as an associative array
|
|
class Person {
|
|
var name: String
|
|
var age: int
|
|
}
|
|
// access properties on Person dynamically via associative array syntax
|
|
var scott = new Person()
|
|
scott["name"] = "Scott"
|
|
scott["age"] = 29
|