Here is the definition of our EmployeeMap once more:
Ice.HashMap type. The mapping also generates a type-specific constructor with the same name as the dictionary. For example:
HashMap objects support the following properties and functions:
This version of the constructor accepts optional comparator functions that the map uses to compare keys and values for equality. If you instantiate a map directly using
new Ice.HashMap()without specifying comparator functions, the default comparators use the
===operator to compare keys and values. As an example, the following map compares its keys and values using
valueComparatorfunction is only used when comparing two maps for equality.
The type-specific constructor generated for a Slice dictionary supplies comparator functions appropriate for its key and value types.
This version of the constructor accepts an optional
HashMapobject from which all entries are (shallow) copied. The new map inherits the comparator functions of the original.
sizeproperty indicates how many entries are currently in the map.
entriesproperty holds the head of a linked list that can be used to traverse all map entries.
Returns the value associated with the given key, or
undefinedif no entry has a matching key.
Adds a new entry to the map having the given key and value. Always returns
Returns true if the map contains an entry with a matching key, or false otherwise.
Removes the entry associated with the given key. Returns the entry's current value if a match was found, or
Removes all entries in the current map.
Returns an array containing all keys in an unspecified order.
Returns an array containing all values in an unspecified order.
Iterates over the entries in the map in an unspecified order, calling the given function once for each entry. The optional thisArg parameter is used as
thiswhen executing the function. The arguments to the function are
value, representing the key and value of each element.
Returns true if this map compares equal to the given map, false otherwise. You can optionally supply a function for
valueComparatorthat the map uses when comparing values; this function takes precedence over the comparator supplied to the map's constructor.
Returns a shallow copy of the map.
Legal key types for
NaN, and any object that defines a
hashCode method. The generated code for a Slice structure that qualifies as a legal dictionary key type includes a
hashCode method. Suppose we define another dictionary type:
The Slice compiler generates a constructor function equivalent to the following code:
Since the key is a user-defined structure type, the map requires a comparator that properly compares keys. Instantiating a map using
new EmployeeDeptMap automatically sets the comparators, whereas calling
new Ice.HashMap in this case would require you to supply your own comparators.
Ice applications should always instantiate a Slice dictionary using the generated constructor.
for..in syntax to iterate over the map's entries. In addition to the
forEach method described above, the map also maintains an unordered linked list of its entries that you can iterate through as shown below:
Each entry object supports the read-only properties
- Attempting to use the
map[key] = valuesyntax to add an element to the map will not have the desired effect; you must use the
- The Ice run time validates the elements of a dictionary to ensure that they are compatible with the declared type; an
Errorexception is raised if an incompatible type is encountered.
HashMapAPI is largely compatible with the draft EcmaScript 6
HashMapwith the standard
Mapwhen it is widely supported.