The inclusion of the Dictionary class in ActionScript 3 creates numerous possibilities when sub classing. The Dictionary class allows for creating key / value mappings or tables which are more commonly known as Hashes or Hash Maps. The only problem is that the Dictionary class does not provide an API for working with the map. The Dictionary class is a good starting point in which sub classes can create managed key / value hash maps and provide methods for retrieving values and keys as well as removing values, determining how many key / values exist and so on.
I am working on a project in which the data set is comprised of sentences tokenized into their individual parts of speech; noun, verb, preposition etc. Part of the requirement is that a lookup can be made on any individual part of speech to retrieve further information such as tense, plurality and so on. At first I simply used a Dictionary for the mappings but ended up adding all kinds of logic outside of the map whenever I needed to check for a key or add a value and so on. So I decided to write a simple HashMap class for AS3 which is similar to the HashMap available in java.util.HashMap.
Below is an example of how to use the HashMap class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import com.ericfeminella.collections.IMap; import com.ericfeminella.collections.HashMap; var map:HashMap = new HashMap(); var o:Object = {name: "HashMap Example"}; map.put('name', o); trace(map.containsKey('name')); //true trace(map.containsValue(o)); //true trace(map.size()); //1 trace(map.getValue('name')); //[Object object] trace(map.isEmpty()); //false map.clear(); trace(map.containsKey('name')); //false trace(map.containsValue(o)); //false trace(map.size()); //0 trace(map.getValue('name')); //false trace(map.isEmpty()); //true |
You can view the source for the HashMap class as well as the IMap interface which defines the methods necessary for working with the HashMap.
{ 28 comments to read ... please submit one more! }