Data Collections

Adding Data

The primary reason for using collections is to store data for future retrieval. This section of the guide focuses on how to add data with several of the primary collections. For more information on other data structures please refer to the Collection Classes section of the user guide.

ArrayList

asdk.ArrayList is the primary class used for storing items in a list. Below is an example of storing items in an ArrayList:

1
2
3
4
var groceryCart:asdk.Collection = new asdk.ArrayList();
groceryCart.addItem( "apple" );
groceryCart.addItem( "orange" );
groceryCart.addItem( "grapes" );
groceryCart.addItem( "apple" );

The addItem method can take any variable type as an argument. The example above demonstrates adding a several strings to the list, but any type can be added. In addition note that an ArrayList can hold duplicate objects.

HashMap

The asdk.HashMap class is used for storing key value pairs and has the benefit of allowing for fast access to stored data. Below is an example of storing items in a HashMap:

1
2
3
4
5
6
7
8
var id1 = 1;
var id2 = 2;
var id3 = 3;

var cache:asdk.Map = new asdk.HashMap();
cache.put( id1, "apple" );
cache.put( id2, "orange" );
cache.put( id3, "grapes" );

Using a HashMap, the put method allows for adding objects based on a key. In the example above several items are added to the map based on ids. This allows for subsequent fast lookups based on those ids so that "grapes" can be retrieved without having to traverse all the elements in the collection.

Stack

The asdk.Stack is a collection in which the last item stored is the first item retrieved (last in first out or LIFO). This type of collection is ideal for simulating a deck of cards where the top card is always the next card that should be delt. Below is an example of how to add items to the Stack class:

1
2
3
4
var cards:asdk.Stack = new asdk.Stack();
cards.push("spades:ace");
cards.push("hearts:queen");
cards.push("clubs:two");

The push method can take any variable type as an argument. The example above demonstrates adding a several strings to the stack the represent cards.

Queue

The asdk.Queue is a collection in which the first item stored is the first item retrieved (first in first out or FIFO). Below is an example of how to add items to the Queue class:

1
2
3
4
var users:asdk.Queue = new asdk.Queue();
users.push("first in line");
users.push("second in line");
users.push("third in line");

The push method can take any variable type as an argument. The example above demonstrates adding a several strings to the queue.

LinkedList

The asdk.LinkedList is a collection in which element maintains a reference to the element before it and the element after it. This kinds of data structure is helpful when needing to traverse both forward and backward within a collection without concern for an elements position. Below is an example of how to add items to a LinkedList:

1
2
3
4
5
6
7
var rainbowColors:LinkedList = new LinkedList();
rainbowColors.addItem( "purple" );
rainbowColors.addItem( "blue" );
rainbowColors.addItem( "green" );
rainbowColors.addItem( "yellow" );
rainbowColors.addItem( "orange" );
rainbowColors.addItem( "red" );

Similar to an ArrayList, a objects are added toa LinkedList via the addItem method.