Skip to content
FutureBlitz

    Install

    Import std.collections.map

    Map

    std.collections.map

    class

    @DebugEntries Public Class Map<K: Hashable, V> Implements Iterable<(K, V)>, Cloneable, Defaultable

    A hash map from keys to values, which keeps its entries in the order they were added.

    Examples

    Local ages = {"Ada": 36, "Alan": 41}
    ages["Grace"] = 85
    Expect(ages.Get("Ada") == 36)
    Expect(ages.Get("Bob") == Null)
    Expect(ages.Keys() == ["Ada", "Alan", "Grace"])

    std/collections/map.fb, line 32

    • Public New() constructor #

      A new, empty Map.

    • Public New(default: V) constructor #

      A Map whose [] gives value for a key that isn't there, without adding the key. It's one value, which every such read gives: a default that's an object or an array is shared, so to give each key one of its own, use GetOrAdd.

    • Public Property Count: Int property #

      How many entries it holds.

    • Public Method Add(key: K, value: V) method #

      Adds an entry, or gives an existing key its new value.

    • Public Method TryAdd(key: K, value: V): Bool method #

      Adds an entry when the key isn't there, and tells whether it wasn't.

    • Public Method AddAll(other: Map<K, V>) method #

      Adds each of other's entries, in its order, as Add does.

    • Public Method Get(key: K): V? method #

      The key's value, or Null when it isn't there.

    • Public Method Get(key: K, fallback: V): V method #

      The key's value, or fallback when it isn't there.

    • Public Method GetOrAdd(key: K, make: Func(): V): V method #

      The key's value, adding what make gives for it first when it isn't there, which is the key's value though make added the key itself.

      Examples

      Local groups = Map<String, Int[]>()
      groups.GetOrAdd("odd", Function(): Int[] => []).Add(1)
      groups.GetOrAdd("odd", Function(): Int[] => []).Add(3)
      Expect(groups["odd"] == [1, 3])
    • Public Method Update(key: K, change: Func(V): V): Bool method #

      Gives the key's value what change gives for it, and tells whether the key was there. A key that change removed stays removed.

      Examples

      Local stock = {"apples": 3}
      Expect(stock.Update("apples", Function(n: Int) => n - 1))
      Expect(!stock.Update("pears", Function(n: Int) => n - 1))
      Expect(stock["apples"] == 2)
    • @PanicsAtCaller Operator [](key: K): V operator #

      The key's value. A key that isn't there gives the Map's default, without adding the key, or panics when the Map has none.

    • Operator []=(key: K, value: V) operator #

      Gives the key a value, adding it when it isn't there.

    • Public Method Contains(key: K): Bool method #

      Whether key has an entry.

    • Public Method Remove(key: K): Bool method #

      Removes the key's entry, and tells whether there was one. The others keep their order.

    • @PanicsAtCaller Public Method RemoveWhere(test: Func(K, V): Bool): Int method #

      Removes each entry that test says to, and tells how many went.

    • Public Method Clear() method #

      Takes every entry out.

    • @PanicsAtCaller Public Method Reserve(count: Int) method #

      Makes room for count entries in all, so adding up to that many doesn't move them again.

    • Public Method Keys(): K[] method #

      Its keys, in order, in a new array.

    • Public Method Values(): V[] method #

      Its values, in order, in a new array.

    • Public Static Method DefaultValue(): Map<K, V> static method #

      A new, empty Map.

    • Public Method Clone(): Map<K, V> method #

      A new Map with the same entries, in the same order, and the same default.

    • @PanicsAtCaller Public Method Filter(keep: Func(K, V): Bool): Map<K, V> method #

      A new Map of the entries that keep says to keep.

    • @PanicsAtCaller Public Method Find(test: Func(K, V): Bool): (K, V)? method #

      The first entry that test says is the one, or Null.

    • @PanicsAtCaller Public Method Any(test: Func(K, V): Bool): Bool method #

      Whether test says yes for any entry, going no further than the first.

    • @PanicsAtCaller Public Method MapValues<U>(change: Func(V): U): Map<K, U> method #

      A new Map of the same keys, in the same order, each with what change gives for its value.

    • Public Method Iterate(): MapIterator<K, V> method #

      Each key and its value, for 'For key, value In map'. Giving a key a new value while going over them is fine, but adding or removing one panics.

    Report an issue with this page or with FutureBlitz on GitHub.