HashMap
Key-value store with string keys.
Methods
| Method | Returns | Description |
|---|---|---|
HashMap.new() | map | Create empty map |
set(key, val) | Set value (string) | |
get(key) | string | Get value ("" if missing) |
has(key) | bool | Key exists |
remove(key) | Delete entry | |
len() | int | Entry count |
keys() | [string] | Array of all keys |
values() | [string] | Array of all values |
clear() | Remove all entries |
Examples
wyn
var m = HashMap.new()
m.set("name", "Wyn")
m.set("version", "2")
println(m.get("name")) // Wyn
println(m.has("name").to_string()) // true
println(m.len().to_string()) // 2Iteration
wyn
var ks = m.keys()
for k in ks {
println("${k}: ${m.get(k)}")
}
// name: Wyn
// version: 2Values
wyn
var vs = m.values()
for v in vs {
println(v)
}Global Access
HashMap variables can be accessed from functions:
wyn
var config = HashMap.new()
fn setup() {
config.set("host", "localhost")
config.set("port", "8080")
}
fn get_url() -> string {
return config.get("host") + ":" + config.get("port")
}
setup()
println(get_url()) // localhost:8080