D has built-in support for associate arrays (AAs) (also known as hash tables).
This is analogous to Java’s
Map and C++’s
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
Here is an example that creates an AA with the squares of numbers from [0, 10] and then displays them to standard output.
int[int] squares; // declaration
for (int i = 0; i <= 10; ++i)
squares[i] = i * i; // insertion to AA
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.
- Re-assigning an already existing key will replace the previous value.
- Attempting to access a key that does not exist, will result in a
Removing from an associative array
remove() function to remove a key.
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.
int* p = 10 in squares;
if (p !is null)
Clearing an associative array
There are two ways to clear an associative array:
- Iterate over the keys and remove them
- Throw away the old AA and create a new one
Method 1 – Remove the keys
foreach (key; aa.keys)
Method 2 – Create a new associative array
To throw away the current AA, assign null to it.
aa = null; // marked for garbage collection
aa = 1; // written to a new memory location
We have already seen some properties of associative arrays, like remove(), and keys. Here are the rest of them:
|.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.|
For more information (e.g. how to use struct or class within associative arrays), read more here [dlang.org].