Maps

A map is a collection that maps keys to values. In Clojure, there are two different types—hashed and sorted.

Hash map

A hash map is the most used map in Clojure. It requires keys that correctly support hashCode and equals.

When should we use it?

They’re useful for:

  • Representing entities

  • When the order isn’t important, and we’re going to perform random access using keys

  • When we don’t want to have duplicated keys

  • Associating keys to values

  • Removing key-value pairs based on the key

  • Fast count

  • Checking if a key exists in the map

How to instantiate a map

We normally instantiate a map using literals. The keys are keywords and need to be unique values.

Get hands-on with 1200+ tech skills courses.