This comparison of programming languages (associative arrays) compares the features of associative array data structures or array-lookup processing for over 40 computer programming languages.
Language support
The following is a comparison of associative arrays (also "mapping", "hash", and "dictionary") in various programming languages.
AWK AWK has built-in, language-level support for associative arrays. For example:
The following code loops through an associated array and prints its contents:
The user can search for elements in an associative array, and delete elements from the array. The following shows how multi-dimensional associative arrays can be simulated in standard AWK using concatenation and the built-in string-separator variable SUBSEP:
C There is no standard implementation of associative arrays in C, but a 3rd-party library, C Hash Table, with BSD license, is available. Another 3rd-party library, uthash, also creates associative arrays from C structures. A structure represents a value, and one of the structure fields serves as the key. Finally, the GLib library also supports associative arrays, along with many other advanced data types and is the recommended implementation of the GNU Project. Similar to GLib, Apple's cross-platform Core Foundation framework provides several basic data types. In particular, there are reference-counted CFDictionary and CFMutableDictionary.
C# C# uses the collection classes provided by the .NET Framework. The most commonly used associative array type is System.Collections.Generic.Dictionary<TKey, TValue>, which is implemented as a mutable hash table. The relatively new System.Collections.Immutable package, available in .NET Framework versions 4.5 and above, and in all versions of .NET Core, also includes the System.Collections.Immutable.Dictionary<TKey, TValue> type, which is implemented using an AVL tree. The methods that would normally mutate the object in-place instead return a new object that represents the state of the original object after mutation.
Creation The following demonstrates three means of populating a mutable dictionary:
the Add method, which adds a key and value and throws an exception if the key already exists in the dictionary; assigning to the indexer, which overwrites any existing value, if present; and assigning to the backing property of the indexer, for which the indexer is syntactic sugar (not applicable to C#, see F# or VB.NET examples).
The dictionary can also be initialized during construction using a "collection initializer", which compiles to repeated calls to Add.
Access by key Values are primarily retrieved using the indexer (which throws an exception if the key does not exist) and the TryGetValue() method, which has an output parameter for the sought value and a Boolean return-value indicating whether the key was found.
In this example, the sallyNumber value will now contain the string "555-9999".
Enumeration A dictionary can be viewed as a sequence of keys, sequence of values, or sequence of pairs of keys and values represented by instances of the KeyValuePair<TKey, TValue> type, although there is no guarantee of order. For a sorted dictionary, the programmer could choose to use a SortedDictionary<TKey, TValue> or use the .Sort LINQ extension method when enumerating. The following demonstrates enumeration using a foreach loop:
C++ In C++, there are four associative array classes: std::map (a red-black tree, equivalent to TreeMap in other languages) and std::unordered_map (a hash table, equivalent to HashMap in other languages). The other two are std::multimap and std::unordered_multimap, which permit multiple entries with the same key. These classes are templated which allow the data types of keys and values to be different for different collection instances. For a given instance of the map class the keys must be of the same base type. The same must be true for all of the values. Although std::map is typically implemented using a self-balancing binary search tree, C++11 defines a second map called std::unordered_map, which has the algorithmic characteristics of a hash table. This is a common vendor extension to the Standard Template Library (STL) as well, usually called hash_map, available from such implementations as SGI and STLPort.
Or less efficiently, as this creates temporary std::string values:
With the extension of initialization lists in C++11, entries can be added during a map's construction as shown below:
You can iterate through the list with the following code (C++03):
The same task in C++11:
Using the structured binding available in C++17:
Cobra Initializing an empty dictionary and adding items in Cobra:
Alternatively, a dictionary can be initialized with all items during construction:
The dictionary can be enumerated by a for-loop, but there is no guaranteed order:
ColdFusion Markup Language A structure in ColdFusion Markup Language (CFML) is equivalent to an associative array:
D D offers direct support for associative arrays in the core language; such arrays are implemented as a hash table with open addressing. The equivalent example would be:
Keys and values can be any types, but all the keys in an associative array must be of the same type, and the same goes for dependent values. Looping through all properties and associated values, and printing them, can be coded as follows:
A property can be removed as follows:
Delphi Delphi supports several standard containers, including TDictionary<T>:
Pre-2009 Delphi versions do not support associative arrays directly. Such arrays can be simulated using the TStrings class:
Erlang Erlang offers many ways to represent mappings; three of the most common in the standard library are keylists, dictionaries, and maps.
Keylists Keylists are lists of tuples, where the first element of each tuple is a key, and the second is a value. Functions for operating on keylists are provided in the lists module.
Accessing an element of the keylist can be done with the lists:keyfind/3 function:
Dictionaries Dictionaries are implemented in the dict module of the standard library. A new dictionary is created using the dict:new/0 function and new key/value pairs are stored using the dict:store/3 function:
Such a serial initialization would be more idiomatically represented in Erlang with the appropriate function:
The dictionary can be accessed using the dict:find/2 function:
… excerpt ends here. Continue reading the full article.