HashMap

The convention is to name the module alias after the file name it is defined in:

import HashMap "mo:base/HashMap";

On this page

Type HashMap.HashMap<K, V>
Class HashMap.HashMap<K, V>

Class methods
    Method get
    Method size
    Method put
    Method replace
    Method delete
    Method remove
    Method keys
    Method vals
    Method entries

Module public functions
Function clone
Function fromIter
Function map
Function mapFilter

Type HashMap.HashMap<K, V>

The HashMap module contains a public type HashMap<K, V> with the same name. It's convenient to rename the type locally:

import HashMap "mo:base/HashMap";

type HashMap<K, V> = HashMap.HashMap<K, V>;

type MapTextInt = HashMap<Text, Int>;

In the second line we declare a local type alias HashMap<K, V> by referring to the type inside the module. This new local type name takes in two generic type parameters <K, V>.

In the third line we declare another local alias MapTextInt which takes no parameters. It is always a HashMap<Text, Int>.

Class HashMap.HashMap<K, V>

HashMap.HashMap<K, V>(

  initCapacity : Nat,
  keyEq : (K, K) -> Bool,
  keyHash : K -> Hash.Hash

)

To construct a hashmap object, we use the HashMap class:

import HashMap "mo:base/HashMap";
import Text "mo:base/Text";

type HashMap<K, V> = HashMap.HashMap<K, V>;

let hashmap : HashMap<Text, Int> = HashMap.HashMap<Text, Int>(5, Text.equal, Text.hash);

Class methods

hashmap.size

func size() : Nat

The function size takes no argument and returns a Nat value.

import HashMap "mo:base/HashMap";
import Text "mo:base/Text";

let map = HashMap.HashMap<Text, Int>(5, Text.equal, Text.hash);

map.put("Rohit", 30);
map.put("Kohli", 28);
map.put("Rahul", 27);

map.size()

hashmap.get

func get(key : K) : (value : ?V)

The function get takes one argument of type K and returns a value of type ?V.

import HashMap "mo:base/HashMap";
import Text "mo:base/Text";

let map = HashMap.HashMap<Text, Int>(5, Text.equal, Text.hash);

map.put("Rohit", 30);
map.put("Kohli", 28);
map.put("Rahul", 27);

map.get("Kohli")

hashmap.put

func put(key : K, value : V)

The function put takes one argument of type K and returns a value of type V.

import HashMap "mo:base/HashMap";
import Text "mo:base/Text";

let map = HashMap.HashMap<Text, Int>(5, Text.equal, Text.hash);

map.put("Rohit", 30);
map.put("Kohli", 28);
map.put("Rahul", 27);

map.put("Surya", 26)

hashmap.replace

func replace(key : K, value : V) : (oldValue : ?V)

The function replace takes one argument of type K and one of type v returns a value of type ?V.

import HashMap "mo:base/HashMap";
import Text "mo:base/Text";

let map = HashMap.HashMap<Text, Int>(5, Text.equal, Text.hash);

map.put("Rohit", 30);
map.put("Kohli", 28);
map.put("Rahul", 27);

map.replace("Rohit", 29)

hashmap.delete

func delete(key : K)

The function delete takes one argument of type K and returns nothing.

import HashMap "mo:base/HashMap";
import Text "mo:base/Text";

let map = HashMap.HashMap<Text, Int>(5, Text.equal, Text.hash);

map.put("Rohit", 30);
map.put("Kohli", 28);
map.put("Rahul", 27);

map.delete("Rahul")

hashmap.remove

func remove(key : K) : (oldValue : ?V)

The function remove takes one argument of type K and returns a value of type ?V.

import HashMap "mo:base/HashMap";
import Text "mo:base/Text";

let map = HashMap.HashMap<Text, Int>(5, Text.equal, Text.hash);

map.put("Rohit", 30);
map.put("Kohli", 28);
map.put("Rahul", 27);

map.remove("Kohli")

hashmap.keys

func keys() : Iter.Iter<K>

The function keys takes nothing and returns an Iterator of type K.

import HashMap "mo:base/HashMap";
import Text "mo:base/Text";
import Iter "mo:base/Iter";

let map = HashMap.HashMap<Text, Int>(5, Text.equal, Text.hash);

map.put("Rohit", 30);
map.put("Kohli", 28);
map.put("Rahul", 27);

let iter : Iter.Iter<Text> = map.keys();

Iter.toArray<Text>(iter);

hashmap.vals

func vals() : Iter.Iter<V>

The function vals takes nothing and returns an Iterator of type V.

import HashMap "mo:base/HashMap";
import Text "mo:base/Text";
import Iter "mo:base/Iter";

let map = HashMap.HashMap<Text, Int>(5, Text.equal, Text.hash);

map.put("Rohit", 30);
map.put("Kohli", 28);
map.put("Rahul", 27);

let iter : Iter.Iter<Int> = map.vals();

Iter.toArray<Int>(iter);

hashmap.entries

func entries() : Iter.Iter<(K, V)>

The function entries takes nothing and returns an Iterator of type tuple (K, V).

import HashMap "mo:base/HashMap";
import Text "mo:base/Text";
import Iter "mo:base/Iter";

let map = HashMap.HashMap<Text, Int>(5, Text.equal, Text.hash);

map.put("Rohit", 30);
map.put("Kohli", 28);
map.put("Rahul", 27);

let iter : Iter.Iter<(Text, Int)> = map.entries();

Iter.toArray<(Text, Int)>(iter);

Module public functions

HashMap.clone

func clone<K, V>(

     map : HashMap<K, V>,
   keyEq : (K, K) -> Bool,
 keyHash : K -> Hash.Hash

  ) : HashMap<K, V>
Parameters
Generic parametersK, V
Variable argumentmap : HashMap<K, V>
Function argument 1keyEq : (K, K) -> Bool
Function argument 2keyHash : K -> Hash.Hash
Return typeHashMap<K, V>
import HashMap "mo:base/HashMap";
import Text "mo:base/Text";

let map = HashMap.HashMap<Text, Int>(5, Text.equal, Text.hash);

map.put("sachin", 100);
map.put("kohli", 74);
map.put("root", 50);

let copy = HashMap.clone(map, Text.equal, Text.hash);

copy.get("kohli")

HashMap.fromIter

func fromIter<K, V>(

         iter : Iter.Iter<(K, V)>,
 initCapacity : Nat,
        keyEq : (K, K) -> Bool,
      keyHash : K -> Hash.Hash

    ) : HashMap<K, V>
Parameters
Generic parametersK, V
Variable argument1iter : Iter.Iter<(K, V)>
Variable argument2initCapacity : Nat
Function argument 1keyEq : (K, K) -> Bool
Function argument 2keyHash : K -> Hash.Hash
Return typeHashMap<K, V>
import HashMap "mo:base/HashMap";
import Iter "mo:base/Iter";
import Text "mo:base/Text";

let array : [(Text, Int)] = [("bitcoin", 1), ("ETH", 2), ("ICP", 20)];

let iter : Iter.Iter<(Text, Int)> = array.vals();

let size : Nat = array.size();

let map : HashMap.HashMap<Text, Int> = HashMap.fromIter<Text, Int>(
    iter,
    size,
    Text.equal,
    Text.hash,
);

map.get("bitcoin")

HashMap.map

func map<K, V1, V2>(

 hashMap : HashMap<K, V1>,
   keyEq : (K, K) -> Bool,
 keyHash : K -> Hash.Hash,
       f : (K, V1) -> V2

   ) : HashMap<K, V2>
Parameters
Generic parametersK, V1, V2
Variable argument1hashMap : HashMap<K, V1>
Function argument 1keyEq : (K, K) -> Bool
Function argument 2keyHash : K -> Hash.Hash
Function argument 3f : (K, V1) -> V2
Return typeHashMap<K, V2>
import HashMap "mo:base/HashMap";
import Text "mo:base/Text";

let map = HashMap.HashMap<Text, Int>(5, Text.equal, Text.hash);

map.put("sachin", 100);
map.put("kohli", 74);
map.put("root", 50);

func edit(k : Text, v : Int) : Int {
    v * 2;
};

let mapping : HashMap.HashMap<Text, Int> = HashMap.map<Text, Int, Int>(
    map,
    Text.equal,
    Text.hash,
    edit,
);

HashMap.mapFilter

func mapFilter<K, V1, V2>(

 hashMap : HashMap<K, V1>,
   keyEq : (K, K) -> Bool,
 keyHash : K -> Hash.Hash,
       f : (K, V1) -> ?V2

   ) : HashMap<K, V2>
Parameters
Generic parametersK, V1, V2
Variable argument1hashMap : HashMap<K, V1>
Function argument 1keyEq : (K, K) -> Bool
Function argument 2keyHash : K -> Hash.Hash
Function argument 3f : (K, V1) -> ?V2
Return typeHashMap<K, V2>
import HashMap "mo:base/HashMap";
import Text "mo:base/Text";

let map = HashMap.HashMap<Text, Int>(5, Text.equal, Text.hash);

map.put("sachin", 100);
map.put("kohli", 74);
map.put("root", 50);

func edit(k : Text, v : Int) : ?Int {
  if (v > 0) {
    ?(v * 2);
  } else {
    null;
  };
};

let mapFilter : HashMap.HashMap<Text, Int> = HashMap.mapFilter<Text, Int, Int>(
  map,
  Text.equal,
  Text.hash,
  edit,
);

mapFilter.get("root")