# How to construct a Bijective map on Motoko?

**URL:** <https://forum.dfinity.org/t/how-to-construct-a-bijective-map-on-motoko/13801>\
**Category:** Developers\
**Tags:** Functional-Programming\
**Created:** [June 14, 2022, 2:55pm UTC](https://forum.dfinity.org/t/how-to-construct-a-bijective-map-on-motoko/13801 "2022-06-14T14:55:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MCLYang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mclyang/32/3447_2.png) [@MCLYang](https://forum.dfinity.org/u/MCLYang)\
**Post date:** [June 14, 2022, 2:55pm UTC](https://forum.dfinity.org/t/how-to-construct-a-bijective-map-on-motoko/13801/1 "2022-06-14T14:55:50Z")

</div>

I am trying to create a object to bound the `Nat` and `Text` variables in Motoko

For example,

```auto
let obj:[(Nat,Text)] = [(0,"a"),(1,"b")];
func foundTextbyNat(_input:Nat):Text;
func foundNatbyText(_input:Text):Nat;

```

the `func foundTextbyNat(_input:Nat):Text;` can be done by initializing a `obj_map:HashMap.HashMap<Nat, Text> = HashMap.fromIter(obj.vals(), 0, Nat.equal, Hash.hash);`, but how about the sec one? Any idea how to create a inversed HashMap? (Hardcode `obj2:[(Text,Nat)] = [("a",0),("b",1)];` is NOT a solution since the `obj` must be dynamic variable)

Furthermore, any data structure we can use to directly create a bijective map? Given key `Nat` return associated `Text` and Given key `Text` return associated `Nat`.

Thank.

---

<div class="post-metadata">

**Author:** ![inviscidpixels](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/inviscidpixels/32/9021_2.png) [@inviscidpixels](https://forum.dfinity.org/u/inviscidpixels)\
**Post date:** [June 14, 2022, 3:10pm UTC](https://forum.dfinity.org/t/how-to-construct-a-bijective-map-on-motoko/13801/2 "2022-06-14T15:10:06Z")

</div>

Can you use a custom hash function that composes the key so that it is bit shifted so the original key and value are distinctly both able to hash to the same key value tuple? Ie get the key’s type max length, shift to concatenate the value, then switch on either to return the same collision yielding the key-value tuple?

---

<div class="post-metadata">

**Author:** ![quint](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/quint/32/40449_2.png) [@quint](https://forum.dfinity.org/u/quint)\
**Post date:** [June 14, 2022, 3:41pm UTC](https://forum.dfinity.org/t/how-to-construct-a-bijective-map-on-motoko/13801/3 "2022-06-14T15:41:00Z")

</div>

Maybe this is a good reference:

> **[GitHub - aviate-labs/bimap.mo: Generic Bijective Maps in Motoko](https://github.com/aviate-labs/bimap.mo)**
>
> Generic Bijective Maps in Motoko. Contribute to aviate-labs/bimap.mo development by creating an account on GitHub.
