AS3 HashMap Update

I have updated the HashMap API to provide additional functionality which comes in handy when working with managed key / value pairs.

Additionally, I have also modified the HashMap class from an “is-a” to a “has-a” relationship as the previous version was a derivation of Dictionary, thus exposing the map and consequently causing a security breach in the API. This has been fixed as of this update as the map is now private rather than the object itself.

The original IMap interface I created was modeled loosely after operations which are typical of the Java Map interface. However I have identified additional operations which are useful when working with key / value pairs. These new operations have been implemented in the latest HashMap class and comprise all of the new methods.

Below is a list of the additional methods which have been implemented in the HashMap update:

  1. getValues: Retrieves all of the values assigned to each key in the specified map
  2. reset: Resets all key assignments in the HashMap instance to null
  3. resetAllExcept: Resets all key / values defined in the HashMap to null with the exception of the
    specified key
  4. clearAllExcept: Clears all key / values defined in the HashMap to null with the exception of the specified key

You can view the latest HashMap API source for the IMap interface and HashMap implementation. Each method is accompanied by a detailed example.

{ 12 comments to read ... please submit one more! }

  1. Hi,

    I want to do something which should be very simple but can’t think which is the best approach.

    I am populating a HashMap with keys and values and now want to pass this into a DataGrid, using the Keys in one column, and maybe massage elements in the value for the second column.

    Can I ask a hand what would be the simplest way to pass the HashMap with say a String for a key and for the value and display the two columns?

    Thanks
    Matt

  2. Spent 5 more minutes and came up with this.
    ———————————————————-

    private function populateDataGrid():void
    {
    dgData = new ArrayCollection();

    var keys = cache.getKeys();
    var values = cache.getValues();
    var noItems:Number = cache.size();

    for(var i:int = 0; i

  3. I have an array of objects coming back from the database. Each object in the array has several key/value pairs. I would like to get to the key not knowing anything about the object. I thought I could do this by setting a Dictionary or maybe your hashmap to the object, but there is no method for this. It seems you have to know the specific key name – I do not know this as it is a generic object coming back and could be different. Basically what I am trying to do is traverse the object in each array element, looking at the key of the key/value pairs in the object. Is there a way I can do this?

    Thanks so much,
    Donna

  4. Well I looked a little deeper into your code and I found putAll() which I tried – here’s an excerpt of my code:

    var tempAssocArray:IMap;
    for (i = 0; i < paymentRefInfo.dt.length; i++)
    {
    tempAssocArray = new HashMap();
    tempAssocArray.putAll(paymentRefInfo.dt[i]);

    I am getting an exception – Type Coercion failed: cannot convert Object@120623a9 to flash.utils.Dictionary

    I followed the example in your comments for this… I really appreciate you sharing your code and I am very grateful for any help you can provide on this issue.

    Thanks,
    Donna

  5. While I am not certain how you are trying to implement this, I would suggest you iterate over each object in the Array using a for…in loop. This is generally the way you would iterate over each property in the object. You could implement the iteration and trace each key and value for each object in the array as follows:

    var array:Array = //reference to your Array of objects
    var n:int = array.length;
    var item:Object = null;

    for ( var i:int = 0; i < n; i++ ) { item = array[i];for ( var key:String in item ) { trace( "key = " + key + "\nvalue = " + item [key] ); } }This will trace all the keys and values for each object in the Array.Hope that helps, Eric

  6. If you want to add each object in the Array to a HashMap you could do so as follows:

    var map:IMap = new HashMap();

    var array:Array = //reference to your Array of objects
    var n:int = array.length;
    var item:Object = null;

    for ( var i:int = 0; i < n; i++ ) { item = array[i];for ( var key:String in item ) { map.put( key, item [key] ); } }The Map will now contain all of the key's and their associated values. Bare in mind that if the objects have the same keys then each subsequent object with the same key will overwrite the previous value mapped to that key.

  7. Thank you Eric. I think you have given me what I need. I really appreciate your help!

    Donna

  8. No problem – glad to help 🙂

  9. I got an error:
    1046: Type was not found or was not a compile-time constant: IHashMapEntry

    can you tell me, please, what i’v done wrong.
    thanks. Dror

  10. hello,
    I am using the HashMap and IMap created by you for data messaging between ActionScript and Java using BlazeDS.recently i have migrated my project from Flex 3 to Flex 4 and i am getting the “Unknown AMF type ’17′” error. Could you please help me out witht this?
    Regards,
    Shashank

  11. I am not sure that this will serialize as, while the underlying storage of the Map is a Dictionary, the Dictionary itself is not exposed. That is, the HashMap essentially wraps a Dictionary instance to provide an API onto it. One possible way to cirumvent this issue would be to refactor the HashMap to Extend Dictionary, thus changing it’s implementation from a “has-a” to an “is-a” relationship. I would give that try.

    Hope that helps,
    Eric

    BTW: I am in the process of publishing some updates to the HashMap API so as to provide Data Bindings capabilities, so that should be coming soon.

{ 0 Pingbacks/Trackbacks }