Map
HashMap can be used to map any type of key to any type of value.
This class offers ordered mapping functionality. That means that the methods getKeys and getValues return the keys and values in the order they were put to the map and that the iterators returned by the methods valueIterator and keyIterator also iterate over the keys and values in the correct order.
This map offers two methods that help you find out whether it contains a specific key or value. These two methods are containsKey and containsValue.
To get the data stored in this map you can use the getKeys, getValues and get methods. If you want to iterate over the values of this map you can use the iterators returned by the methods iterator or valueIterator. If you want to iterate over the keys you can use the iterator returned by the keyIterator method.
To add key-value pairs to this map you can use the methods put and
putAll. The putAll method lets you add all key-value pairs
contained in the passed-in map to this map.
To remove key-value pairs you can use the methods remove and
clear. The remove method deletes only the key-value pair
corresponding to the passed-in key, while the clear method removes all
key-value pairs.
There are two more methods you may need. The isEmpty and the size method. These methods give you information about whether this map contains any mappings and how many mappings it contains.
To change the string representation returned by the toString method you can set your own stringifier using the static AbstractMap.setStringifier method.
Example:
// constructs the map
var key1:Object = new Object();
var key2:Object = new Object();
var key3:Object = new Object();
var map:Map = new HashMap();
map.put(key1, "value1");
map.put(key2, "value2");
map.put(key3, "value3");
// uses the map
trace(map.get(key1));
trace(map.get(key2));
trace(map.get(key3));
Output:
value1 value2 value3
new HashMap()public function HashMap(source)
Constructs a new HashMap instance.
This map iterates over the passed-in source with the for..in loop and uses the variables' names as key and their values as value. Variables that are hidden from for..in loops will not be added to this map.
source | (optional) an object that contains key-value pairs to populate this map with |
public function containsKey(key):Boolean
Checks if the passed-in key exists.
That means whether a value has been mapped to it.
key | the key to be checked for availability |
true if the key exists else false
public function containsValue(value):Boolean
Checks if the passed-in value is mapped to a key.
value | the value to be checked for availability |
true if the value is mapped to a key else false
public function getKeys(Void):ArrayReturns an array that contains all keys that have a value mapped to it.
an array that contains all keys
public function getValues(Void):ArrayReturns an array that contains all values that are mapped to a key.
an array that contains all mapped values
public function get(key)
Returns the value that is mapped to the passed-in key.
key | the key to return the corresponding value for |
the value corresponding to the passed-in key
public function put(key, value)
Maps the given key to the value.
Both key and value are allowed to be null and
undefined.
key | the key used as identifier for the value
|
value | the value to map to the key
|
the value that was originally mapped to the key or undefined
public function putAll(map:Map):Void
Copies all mappings from the passed-in map to this map.
map | the mappings to add to this map |
public function remove(key)
Removes the mapping from the given key to the value.
key | the key identifying the mapping to remove |
the value that was originally mapped to the key
public function iterator(Void):IteratorReturns an iterator to iterate over the values of this map.
an iterator to iterate over the values of this map
iterator() in org.as2lib.data.holder.Map
public function valueIterator(Void):IteratorReturns an iterator to iterate over the values of this map.
an iterator to iterate over the values of this map
valueIterator() in org.as2lib.data.holder.Map
public function keyIterator(Void):IteratorReturns an iterator to iterate over the keys of this map.
an iterator to iterate over the keys of this map
keyIterator() in org.as2lib.data.holder.Map
public function size(Void):NumberReturns the amount of mappings.
the amount of mappings
public function isEmpty(Void):BooleanReturns whether this map contains any mappings.
true if this map contains no mappings else false
public function toString():StringReturns the string representation of this map.
The string representation is obtained using the stringifier returned by the static AbstractMap.getStringifier method.
the string representation of this map
toString() in org.as2lib.core.BasicInterface