In AS3, where do you draw the line between Dictionary and ArrayCollection?

I basically used the Dictionary object in my program, which basically used ints as its keys and stored the peer RTMFP identifiers in the appropriate places. Each int was unique and represented one user.

Now I need to extend this when users are identified by a combination of int and Boolean, sort of like:

private var m_iUID:int; private var m_blnIsCurrent:Boolean; 

Only the combination between the two truly uniquely identifies the user. It was said that I was about to use a new class made from this for dictionary keys; but then it occurred to me that instead of doing it this way, I could just add the peer identifier to the class definition and turn the Dictionary object into an ArrayCollection:

 private var m_iUID:int; private var m_blnIsCurrent:Boolean; public var m_strNearID:String; 

So now I am wondering what is really better in this scenario. And this question led to a bigger question: where do you really draw the line between these two types of collections as a whole? In the end, they suddenly start to seem completely different, except when you are trying to avoid clutter with class definitions. Probably, I really ask for advice both on a specific scenario and on a general issue. Thanks!

0
source share
2 answers

ArrayCollection is just a wrapper for an array and is only available in Flex.

In AS3, you really have 3 main types of hash tables: Array, Object, and Dictionary. You choose which one to use based on the type of key you want to use: an integer, a string, or an object reference. Arrays converts any key to int, Object converts any key to a string. The dictionary works like an Object for string keys (and converts primitives to string), but what it really does good uses object references as keys.

You want to use a single int as a unique key, use an array. If you want to use a single line as a unique key, use an object. If you want to use object references as a unique key, use a dictionary.

In your case, you should probably use the object and your own toString() method in your key class. This is because you want to use a composition of primitive values ​​(NOT reference) as a unique key. It is not possible to do this initially, so you have to crush the values ​​together as a single line. Objects are the best (fastest) hash table for string keys, so this is the collection you should use.

Example:

 class User { private var m_iUID:int; private var m_blnIsCurrent:Boolean; public var m_strNearID:String; public function User(UID:int, IsCurrent:Boolean) { m_iUID = UID; m_blnIsCurrent = IsCurrent; } // Custom toString to mash together primitives public function toString() { return m_iUID.toString() + "-" + (m_blnIsCurrent ? "1" : "0"); } } // Later: var allUsers:Object = {} var user1:User = new User(231049, true); var user2:User = new User(0x2309, false); // Implicitly calls toString(): allUsers[user1] = "User 1"; allUsers[user2] = "User 2"; // All of the following will successfully retrieve the value for user1 ("User 1"): // ONLY the first would work if allUsers was a Dictionary trace(allUsers[user1]); trace(allUsers[user1.toString()]); trace(allUsers["231049-1"]); trace(allUsers[new User(231049, true)]); 
+4
source

Dictionary and ArrayCollection have some important differences:

  • Dictionary matches objects with other objects, and ArrayCollection is just a list of objects.
  • ArrayCollection only for Flex, therefore unsuitable for use in a common AS3 project.

Which one you should use, really depends on what you need in your application:

  • Will you use the "identity" object (with user id and "current") elsewhere without an associated peer identifier? In this case, make it a separate Identity class or so, and use Dictionary to map Identity instances to peer identifiers.
  • Do you need to search based on identifiers? In other words, you need to ask "what peer identifier is associated with this identifier?". If so, go over Dictionary + Identity again to avoid iterating over the list.

I am sure there are more considerations, but they should start.

+1
source

All Articles