views:

152

answers:

3

I'm relatively new to flash, and is confused about what I should use to store and retrieve key value pairs. After some googling I've found various map-like things to choose from:

1) Use a Object:

var map:Object = new Object();
map["key"] = "value";

The problem is that it seems to lack some very basic features. For example to even get the size of map I'd have to write a util method.

2) Use a Dictionary

What does this standard library class provide over the simple object? It seems silly for it to exist if it's functionally identical to Object.

3) Go download some custom HashMap/HashTable implementation from the web.

I've used a lot of modern languages, and this is the first time I haven't been able to find a library implementation of an associative array within 5 minutes. So I'd like to get some best-practice advice from an experienced flash developer.

Thanks!

A: 

Have a look at ds - data structures. This is a further development of AS3DS.

aeby
+1  A: 

Maybe your google foo is a bit weak today?

But you're right, the built-in Object object, doesn't provide many extra features.

Dictionaries have at least two important differences with Objects:

They can use any object as a key. For Objects, the key has to be a string (if you pass any other object, the toString() method will be implicitly called).

You can optionally set they keys to be weak referenced (this doesn't make much sense for Objects).

Anyway, there are a number of opensource libraries that implement various data structures and collection types.

Just from the top of my head:

http://lab.polygonal.de/ds/

http://sibirjak.com/blog/index.php/collections/as3commons-collections/

Juan Pablo Califano
I came across as3commons while googling but thought it might be overkill to download a whole collections library. But will try it out as the project seems pretty legit, with lots of unit tests and examples, last updated just a few days ago.
tstyle
A: 

You should use whichever option is needed for a particular situation. There is no correct answer to say "always use 'x'".

I've found that the vast majority of times Object based dictionaries are all that's needed. They're extremely fast and easy to use and I almost never need the extra features. It converts any key to a string which works well for most situations.

Dictionary provides some extra features but I've never needed object-based keys (and I've been programming in Flex since 1.0 alpha 1). The only time I've used a Dictionary is as a hack to get access to a weak reference since Flex doesn't provide a simple weak reference class.

More complex dictionaries are available which will provide more functionality. If you really need this functionality, then they will be useful, but I wouldn't recommend jumping in to use them when a plain old Object will work find for your needs. That said, if you do turn out to need them in your application, it might be best to using the same 3rd-party dictionary everywhere in that app for consistency and easy of maintenance.

Sam