Thursday, May 27, 2010

Flex Actionscript GoodNess: AS equivalent of Java Hashmap / Hashtable

With Java being the my core language, it always puzzles me why is there is not good Hashtable functionality..

There are lot of equivalents though, but I still miss the Java Hashtable.

Here are the Options

1. Associative Arrays

   1: var params:Array = new Array()
   2: params.firstName = txName.text
   3: params.lastName = txLName.text
   4: displayValues(params)
   5:  
   6: public static const FIRST_NAME:String ="firstName"
   7: public static const LAST_NAME:String = "lastName"
   8: //Retreive values From Associative Array
   9: public function displayValues(params:Array):void{
  10:     var firstName:String = params[FIRST_NAME]
  11:     var lastName:String = params[LAST_NAME]
  12:     trace(firstName)
  13:     trace(lastName)
  14: }





2. flash.utils.Dictionary



   1: import flash.utils.Dictionary
   2:  
   3: //Set values to dictionary
   4: public function dictionaryDemo(fName:String, lName:String):void{
   5:     var aDict:Dictionary = new Dictionary()
   6:     aDict.firstName = fName
   7:     aDict.lastName = lName
   8:      displayValues(aDict)
   9: }
  10: public function displayValues(aDict:Dictionary):void{
  11:     trace(aDict.firstName)
  12:     trace(aDict.lastName)
  13:  
  14: //You can also iterate over a Dictionary
  15:     for (val in aDict) { 
  16:         trace ("KeyVal pair is " + val + " = " + aDict[val]); 
  17:     }
  18: }
  19:  


What do you guys think, which one is better Associative Arrays or Dictionary..



By default, I have observed that actionscript Object type behaves like dictionary.

No comments:

Post a Comment