# HashMap\[K: [Any](builtin-Any) #share, V: [Any](builtin-Any) #share, H: [HashFunction](collections-HashFunction)\[K\] val\]
<span class="source-link">[[Source]](src/collections-persistent/map.md#L14)</span>

A persistent map based on the Compressed Hash Array Mapped Prefix-tree from
'Optimizing Hash-Array Mapped Tries for Fast and Lean Immutable JVM
Collections' by Michael J. Steindorfer and Jurgen J. Vinju

## Usage
```pony
use "collections/persistent"

actor Main
  new create(env: Env) =>
    try
      let m1 = Map[String, U32] // {}
      // Update returns a new map with the provided key set
      // to the provided value. The old map is unchanged.
      let m2 = m1("a") = 5 // {a: 5}
      let m3 = m2("b") = 10 // {a: 5, b: 10}
      let m4 = m3.remove("a")? // {b: 10}
      // You can create a new map from key value pairs.
      let m5 = Map[String, U32].concat([("a", 2); ("b", 3)].values()) // {a: 2, b: 3}
    end
```


```pony
class val HashMap[K: Any #share, V: Any #share, H: HashFunction[K] val]
```

## Constructors

### create
<span class="source-link">[[Source]](src/collections-persistent/map.md#L41)</span>


```pony
new val create()
: HashMap[K, V, H] val^
```

#### Returns

* [HashMap](collections-persistent-HashMap)\[K, V, H\] val^

---

### _create
<span class="source-link">[[Source]](src/collections-persistent/map.md#L45)</span>


```pony
new val _create(
  r: _MapNode[K, V, H] val,
  s: USize val)
: HashMap[K, V, H] val^
```
#### Parameters

*   r: [_MapNode](collections-persistent-_MapNode)\[K, V, H\] val
*   s: [USize](builtin-USize) val

#### Returns

* [HashMap](collections-persistent-HashMap)\[K, V, H\] val^

---

## Public Functions

### apply
<span class="source-link">[[Source]](src/collections-persistent/map.md#L49)</span>


Attempt to get the value corresponding to k.


```pony
fun val apply(
  k: K)
: val->V ?
```
#### Parameters

*   k: K

#### Returns

* val->V ?

---

### size
<span class="source-link">[[Source]](src/collections-persistent/map.md#L55)</span>


Return the amount of key-value pairs in the Map.


```pony
fun val size()
: USize val
```

#### Returns

* [USize](builtin-USize) val

---

### update
<span class="source-link">[[Source]](src/collections-persistent/map.md#L61)</span>


Update the value associated with the provided key.


```pony
fun val update(
  key: K,
  value: val->V)
: HashMap[K, V, H] val
```
#### Parameters

*   key: K
*   value: val->V

#### Returns

* [HashMap](collections-persistent-HashMap)\[K, V, H\] val

---

### remove
<span class="source-link">[[Source]](src/collections-persistent/map.md#L74)</span>


Try to remove the provided key from the Map.


```pony
fun val remove(
  k: K)
: HashMap[K, V, H] val ?
```
#### Parameters

*   k: K

#### Returns

* [HashMap](collections-persistent-HashMap)\[K, V, H\] val ?

---

### get_or_else
<span class="source-link">[[Source]](src/collections-persistent/map.md#L80)</span>


Get the value associated with provided key if present. Otherwise,
return the provided alternate value.


```pony
fun val get_or_else(
  k: K,
  alt: val->V)
: val->V
```
#### Parameters

*   k: K
*   alt: val->V

#### Returns

* val->V

---

### contains
<span class="source-link">[[Source]](src/collections-persistent/map.md#L91)</span>


Check whether the node contains the provided key.


```pony
fun val contains(
  k: K)
: Bool val
```
#### Parameters

*   k: K

#### Returns

* [Bool](builtin-Bool) val

---

### concat
<span class="source-link">[[Source]](src/collections-persistent/map.md#L102)</span>


Add the K, V pairs from the given iterator to the map.


```pony
fun val concat(
  iter: Iterator[(val->K , val->V)] ref)
: HashMap[K, V, H] val
```
#### Parameters

*   iter: [Iterator](builtin-Iterator)\[(val->K , val->V)\] ref

#### Returns

* [HashMap](collections-persistent-HashMap)\[K, V, H\] val

---

### keys
<span class="source-link">[[Source]](src/collections-persistent/map.md#L112)</span>


```pony
fun val keys()
: MapKeys[K, V, H] ref
```

#### Returns

* [MapKeys](collections-persistent-MapKeys)\[K, V, H\] ref

---

### values
<span class="source-link">[[Source]](src/collections-persistent/map.md#L114)</span>


```pony
fun val values()
: MapValues[K, V, H] ref
```

#### Returns

* [MapValues](collections-persistent-MapValues)\[K, V, H\] ref

---

### pairs
<span class="source-link">[[Source]](src/collections-persistent/map.md#L116)</span>


```pony
fun val pairs()
: MapPairs[K, V, H] ref
```

#### Returns

* [MapPairs](collections-persistent-MapPairs)\[K, V, H\] ref

---

## Private Functions

### _root_node
<span class="source-link">[[Source]](src/collections-persistent/map.md#L118)</span>


```pony
fun box _root_node()
: _MapNode[K, V, H] val
```

#### Returns

* [_MapNode](collections-persistent-_MapNode)\[K, V, H\] val

---

