Associative arrays

D has built-in support for associate arrays (AAs) (also known as hash tables).

This is analogous to Java’s Map and C++’s std::unordered_map.

Declaring an associative array

To declare an associative array, use the following syntax:

Inserting into an associative array

To insert into an AA, use the [] operator.
Here is an example that creates an AA with the squares of numbers from [0, 10] and then displays them to standard output.

Running the above displays:
[0:0, 6:36, 7:49, 2:4, 3:9, 10:100, 1:1, 8:64, 5:25, 4:16, 9:81]
Note that the numbers are not sorted — This is expected, as associative arrays are not internally sorted.

Notes

  • Re-assigning an already existing key will replace the previous value.
  • Attempting to access a key that does not exist, will result in a core.exception.RangeError error.

Removing from an associative array

Use the remove() function to remove a key.
aa.remove("hello");

Testing if a key exists

To test if a key exists, use the in operator, which returns a pointer to the value. If the key doesn’t exist, the pointer is null.

Clearing an associative array

There are two ways to clear an associative array:

  1. Iterate over the keys and remove them
  2. Throw away the old AA and create a new one

Method 1 – Remove the keys

Method 2 – Create a new associative array

To throw away the current AA, assign null to it.

Properties

We have already seen some properties of associative arrays, like remove(), and keys. Here are the rest of them:

Property Description
.sizeof Returns the size of the reference to the associative array; it is 4 in 32-bit builds and 8 on 64-bit builds.
.length Returns number of values in the associative array. Unlike for dynamic arrays, it is read-only.
.dup Create a new associative array of the same size and copy the contents of the associative array into it.
.keys Returns dynamic array, the elements of which are the keys in the associative array.
.values Returns dynamic array, the elements of which are the values in the associative array.
.rehash Reorganizes the associative array in place so that lookups are more efficient. rehash is effective when, for example, the program is done loading up a symbol table and now needs fast lookups in it. Returns a reference to the reorganized array.
.byKey() Returns a forward range suitable for use as a ForeachAggregate to a ForeachStatement which will iterate over the keys of the associative array.
.byValue() Returns a forward range suitable for use as a ForeachAggregate to a ForeachStatement which will iterate over the values of the associative array.
.byKeyValue() Returns a forward range suitable for use as a ForeachAggregate to a ForeachStatement which will iterate over key-value pairs of the associative array. The returned pairs are represented by an opaque type with .key and .value properties for accessing the key and value of the pair, respectively.
.get(Key key, lazy Value defVal) Looks up key; if it exists returns corresponding value else evaluates and returns defVal.

Further reading

For more information (e.g. how to use struct or class within associative arrays), read more here [dlang.org].