To successfully store and retrieve (key->value) mapping from a HashMap. HashMap accept any type of object to be the key: number, string, Object etc... But it is only get fast accessing with string type keys. Others are slow.
---------------------------------------------------------- This example creates a HashMap of friends. It uses the number of the friends as keys:
function person(name,age,sex){ this.name=name; this.age=age; this.sex=sex; } var friends = new HashMap(); friends.put("one", new person("iiley",21,"M")); friends.put("two", new person("gothic man",22,"M")); friends.put("three", new person("rock girl",19,"F"));
To retrieve a friends, use the following code:
var thisperson = friends.get("two"); if (thisperson != null) { trace("two name is "+thisperson.name); trace("two age is "+thisperson.age); trace("two sex is "+thisperson.sex); }else{ trace("two is not in friends!"); }
new HashMap()
public function containsValue(value):Boolean
Tests if some key maps into the specified value in this HashMap. This operation is more expensive than the containsKey method.
public function containsKey(key):Boolean
Tests if the specified object is a key in this HashMap. This operation is very fast if it is a string.
key | The key whose presence in this map is to be tested |
true if this map contains a mapping for the specified
public function get(key)
Returns the value to which the specified key is mapped in this HashMap. Return null if the key is not mapped to any value in this HashMap. This operation is very fast if the key is a string.
key | the key whose associated value is to be returned. |
the value to which this map maps the specified key, or null if the map contains no mapping for this key or it is null value originally.
public function put(key, value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.
key | key with which the specified value is to be associated. |
value | value to be associated with the specified key. |
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the HashMap previously associated null with the specified key.
public function remove(key)
Removes the mapping for this key from this map if present.
key | key whose mapping is to be removed from the map. |
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key.