Share 'Examples' on Delicious Share 'Examples' on Facebook Share 'Examples' on Google Bookmarks Share 'Examples' on Twitter

  1. ArrayList - Usage
  2. SortedList - Usage
  3. Set - Usage
  4. LinkedSet - Usage
  5. SortedSet - Usage
  6. Map - Usage
  7. LinkedMap - Usage
  8. SortedMap - Usage
  9. LinkedList - Usage
  10. Treap - Usage
  11. ArrayListFx - Bindable ArrayList
  12. SortedListFx - Bindable SortedList
  13. SetFx - Bindable Set
  14. LinkedSetFx - Bindable LinkedSet
  15. SortedSetFx - Bindable SortedSet
  16. MapFx - Bindable Map
  17. LinkedMapFx - Bindable LinkedMap
  18. SortedMapFx - Bindable SortedMap
  19. CollectionEvent Polymorphy - Type Casting
  20. CollectionEvent Iterator - Locating changes
  21. CollectionEvent Iterator Polymorphy - Type Casting
  22. All Examples
    1. Collections
    2. Iterators
    3. Bindable Collections
    4. Collection Events
    5. Special Iterators
    6. Sorted Collections
    7. Tools and Utils
  23. Comments (2)
  24. Leave a Comment

Examples

Categories: Collection (10), Bindable Collection (11), List (7), Set (9), Map (9), Iterator (9), ArrayList (2), SortedList (2), LinkedSet (2), SortedSet (2), LinkedMap (2), SortedMap (2), Collection Event (3), Tools and Utils (12), Special Iterators (4), Sorted Collections (8)

ArrayList – Usage

The example shows the usage of the methods of the ArrayList collection.

ArrayListExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package {
  import org.as3commons.collections.ArrayList;
  import org.as3commons.collections.framework.ICollectionIterator;
  import org.as3commons.collections.framework.IIterator;
  import org.as3commons.collections.framework.IListIterator;
  import org.as3commons.collections.framework.IOrderedListIterator;
  import org.as3commons.collections.utils.StringComparator;
  import flash.display.Sprite;

  public class ArrayListExample extends Sprite {

    public function ArrayListExample() {
      var list : ArrayList = new ArrayList();
     
      // Add

      list.add("a");
      list.add("b");
      list.addAllAt(1, ["c", "d"]);
      trace (list.toArray()); // a,c,d,b

      list.addFirst("e");
      list.addLast("a");
      list.addAt(3, "a");
      trace (list.toArray()); // e,a,c,a,d,b,a
     
      // Replace

      list.replaceAt(2, "a");
      list.replaceAt(3, "f");
      trace (list.toArray()); // e,a,a,f,d,b,a

      // Inspection

      trace (list.size); // 7
      trace (list.has("b")); // true
      trace (list.itemAt(3)); // f
      trace (list.firstIndexOf("a")); // 1
      trace (list.lastIndexOf("a")); // 6
      trace (list.count("a")); // 3
     
      // Reorder

      list.reverse();
      trace (list.toArray()); // a,b,d,f,a,a,e

      list.sort(new StringComparator());
      trace (list.toArray()); // a,a,a,b,d,e,f
     
      // Iterator

      var iterator : IIterator = list.iterator();
      trace (iterator is IIterator); // true
      trace (iterator is ICollectionIterator); // true
      trace (iterator is IListIterator); // true
      trace (iterator is IOrderedListIterator); // true

      while (iterator.hasNext()) {
        trace (iterator.next()); // a,a,a,b,d,e,f
      }

      // List iterator

      var listIterator : IListIterator = list.iterator() as IListIterator;
      while (listIterator.hasNext()) {
        listIterator.next();
        trace (listIterator.index + "=" + listIterator.current);
      }

      // 0=a
      // 1=a
      // 2=a
      // 3=b
      // 4=d
      // 5=e
      // 6=f

      // Remove

      list.remove("a");
      trace (list.toArray()); // a,a,b,d,e,f

      list.removeAll("a");
      trace (list.toArray()); // b,d,e,f

      list.removeAt(1);
      list.removeFirst();
      list.removeLast();
      trace (list.toArray()); // e

      list.clear();
      trace (list.toArray()); // []
    }
  }
}

SortedList – Usage

This example shows the specific behaviour of a SortedList. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

SortedListExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package {
  import org.as3commons.collections.SortedList;
  import org.as3commons.collections.framework.ICollectionIterator;
  import org.as3commons.collections.framework.IIterator;
  import org.as3commons.collections.framework.IListIterator;
  import org.as3commons.collections.utils.StringComparator;
  import flash.display.Sprite;

  public class SortedListExample extends Sprite {

    public function SortedListExample() {
      var list : SortedList = new SortedList(new StringComparator());
     
      // Add

      list.add("d");
      list.add("a");
      list.add("a");
      list.add("f");
      list.add("c");
      list.add("b");
      list.add("a");
      list.add("c");
      trace (list.toArray()); // a,a,a,b,c,c,d,f

      list.array = ["d", "a", "a", "f", "c", "b", "a", "c"];
      trace (list.toArray()); // a,a,a,b,c,c,d,f

      // Inspection

      trace (list.first); // a
      trace (list.last); // f

      trace (list.hasEqual("a")); // true
      trace (list.hasEqual("e")); // false
      trace (list.equalIndices("a")); // 0,1,2

      trace (list.lesserIndex("a")); // -1
      trace (list.lesserIndex("c")); // 3
     
      trace (list.higherIndex("c")); // 6
      trace (list.higherIndex("f")); // -1
      trace (list.higherIndex("x")); // -1

      // Iterator
     
      var iterator : IIterator = list.iterator();
      trace (iterator is IIterator); // true
      trace (iterator is ICollectionIterator); // true
      trace (iterator is IListIterator); // true
     
      while (iterator.hasNext()) {
        trace (iterator.next()); // a,a,a,b,c,c,d,f
      }
    }
  }
}

Set – Usage

This example shows the specific behaviour of a Set. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

Category: Set, Collection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package {
  import org.as3commons.collections.Set;
  import org.as3commons.collections.framework.ICollectionIterator;
  import org.as3commons.collections.framework.IIterator;
  import org.as3commons.collections.framework.ISetIterator;
  import flash.display.Sprite;

  public class SetExample extends Sprite {

    public function SetExample() {
      var theSet : Set = new Set();
     
      // Add
     
      theSet.add(1);
      theSet.add(2);
      theSet.add(3);
      theSet.add("4a");
      theSet.add("4b");
      theSet.add("4b"); // no effect (item already contained)
     
      trace (theSet.toArray()); // 4a,4b,1,2,3
     
      // Inspection
     
      trace (theSet.has(1)); // true;
      trace (theSet.has(4)); // false;
     
      // Iterator

      var iterator : IIterator = theSet.iterator();
      trace (iterator is IIterator); // true
      trace (iterator is ICollectionIterator); // true
      trace (iterator is ISetIterator); // true

      while (iterator.hasNext()) {
        trace (iterator.next()); // 4a,4b,1,2,3
      }

      // Set iterator

      var si : ISetIterator = theSet.iterator() as ISetIterator;
      trace (si.previousItem + " [" + si.current + "] " + si.nextItem);
      while (si.hasNext()) {
        si.next();
        trace (si.previousItem + " [" + si.current + "] " + si.nextItem);
      }

      // undefined [undefined] 4b
      // 4b [4b] 4a
      // 4a [4a] 1
      // 1 [1] 2
      // 2 [2] 3
      // 3 [3] undefined

      // Remove
     
      theSet.remove(2);
      theSet.remove("4b");
      theSet.remove("4"); // no effect (item not contained)

      trace (theSet.toArray()); // 4a,1,3
    }
  }
}

LinkedSet – Usage

This example shows the specific behaviour of a LinkedSet. The general work with a set is more detailed illustrated in the related Set example. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

LinkedSetExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package {
  import org.as3commons.collections.LinkedSet;
  import org.as3commons.collections.framework.ICollectionIterator;
  import org.as3commons.collections.framework.IIterator;
  import org.as3commons.collections.framework.IMapIterator;
  import org.as3commons.collections.framework.IOrderedMapIterator;
  import flash.display.Sprite;

  public class LinkedSetExample extends Sprite {

    public function LinkedSetExample() {
      var theSet : LinkedSet = new LinkedSet();
     
      // Add
     
      theSet.add(4);
      theSet.add(1);
      theSet.add(3);
      theSet.add(5);
      theSet.add(2);
      trace (theSet.toArray()); // 4,1,3,5,2
     
      theSet.addBefore(3, 6);
      theSet.addAfter(5, 7);
      trace (theSet.toArray()); // 4,1,6,3,5,7,2

      theSet.addFirst(8);
      theSet.addLast(9);
      trace (theSet.toArray()); // 8,4,1,6,3,5,7,2,9

      // Inspection
     
      trace (theSet.first); // 8
      trace (theSet.last); // 9
     
      // Iterator

      var iterator : IIterator = theSet.iterator();
      trace (iterator is IIterator); // true
      trace (iterator is ICollectionIterator); // true
      trace (iterator is IMapIterator); // true
      trace (iterator is IOrderedMapIterator); // true
     
      while (iterator.hasNext()) {
        trace (iterator.next()); // 8,4,1,6,3,5,7,2,9
      }

      // Remove

      theSet.removeFirst();
      theSet.removeFirst();
      theSet.removeLast();
      theSet.removeLast();
      trace (theSet.toArray()); // 1,6,3,5,7
    }
  }
}

SortedSet – Usage

This example shows the specific behaviour of a SortedSet. The general work with a set is more detailed illustrated in the related Set example. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

SortedSetExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package {
  import org.as3commons.collections.SortedSet;
  import org.as3commons.collections.framework.ICollectionIterator;
  import org.as3commons.collections.framework.IIterator;
  import org.as3commons.collections.framework.ISetIterator;
  import flash.display.Sprite;

  public class SortedSetExample extends Sprite {

    public function SortedSetExample() {
      var theSet : SortedSet = new SortedSet(new PersonComparator());
      var erikaMeier : Person = new Person("Erika", "Meier");
     
      // Add
     
      theSet.add(new Person("Hans", "Meier"));
      theSet.add(new Person("Erika", "Meier"));
      theSet.add(new Person("Ernst", "Schmitt"));
      theSet.add(new Person("Anton", "Fischer"));
      theSet.add(new Person("Erika", "Becker"));
      theSet.add(erikaMeier);
      theSet.add(erikaMeier); // no effect (item already contained)
     
      trace (theSet.toArray());
     
      // Erika Becker
      // Anton Fischer
      // Erika Meier
      // Erika Meier
      // Hans Meier
      // Ernst Schmitt

      // Inspect
     
      trace (theSet.has(new Person("Erika", "Meier"))); // false (item not contained)
      trace (theSet.has(erikaMeier)); // true

      trace (theSet.first); // Erika Becker
      trace (theSet.last); // Ernst Schmitt

      trace (theSet.hasEqual(new Person("Ernst", "Schmitt"))); // true
      trace (theSet.hasEqual(new Person("Erika", "Schmitt"))); // false
      trace (theSet.equalItems(new Person("Erika", "Meier"))); // Erika Meier, Erika Meier

      trace (theSet.lesser(new Person("Erika", "Becker"))); // undefined
      trace (theSet.lesser(new Person("Karla", "Dieling"))); // Erika Becker
      trace (theSet.lesser(new Person("Erika", "Meier"))); // Anton Fischer
     
      trace (theSet.higher(new Person("Erika", "Meier"))); // Hans Meier
      trace (theSet.higher(new Person("Anna", "Pauli"))); // Ernst Schmitt
      trace (theSet.higher(new Person("Ernst", "Schmitt"))); // undefined
      trace (theSet.higher(new Person("Wolfgang", "Teske"))); // undefined

      // Iterator

      var iterator : IIterator = theSet.iterator();
      trace (iterator is IIterator); // true
      trace (iterator is ICollectionIterator); // true
      trace (iterator is ISetIterator); // true
     
      while (iterator.hasNext()) {
        trace (iterator.next());
      }

      // Erika Becker
      // Anton Fischer
      // Erika Meier
      // Erika Meier
      // Hans Meier
      // Ernst Schmitt

      // Remove
     
      theSet.remove(erikaMeier);
      theSet.remove(erikaMeier); // no effect (item not contained)
     
      var ernstFischer : Person = theSet.lesser(new Person("Erika", "Meier"));
      theSet.remove(ernstFischer);

      trace (theSet.toArray());
     
      // Erika Becker
      // Erika Meier
      // Hans Meier
      // Ernst Schmitt

      theSet.removeFirst();
      theSet.removeLast();

      trace (theSet.toArray());
     
      // Erika Meier
      // Hans Meier
    }
  }
}

import org.as3commons.collections.utils.StringComparator;

internal class Person {
  public var firstName : String;
  public var lastName : String;
  public function Person(first : String, last : String) {
    firstName = first;
    lastName = last;
  }
  public function toString() : String {
    return firstName + " " + lastName;
  }
}

internal class PersonComparator extends StringComparator {
  override public function compare(item1 : *, item2 : *) : int {
    var compare : int = super.compare(
      Person(item1).lastName, Person(item2).lastName
    );
    if (compare) return compare;
    return super.compare(
      Person(item1).firstName, Person(item2).firstName
    );
  }
}

Map – Usage

This example shows the specific behaviour of a Map. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

Category: Map, Collection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package {
  import org.as3commons.collections.Map;
  import org.as3commons.collections.framework.ICollectionIterator;
  import org.as3commons.collections.framework.IIterator;
  import org.as3commons.collections.framework.IMapIterator;
  import flash.display.Sprite;

  public class MapExample extends Sprite {

    public function MapExample() {
      var map : Map = new Map();
     
      // Add
     
      map.add(1, "one");
      map.add(2, "two");
      map.add(3, "three");
      map.add("4a", "four");
      map.add("4b", "four");
      map.add("4c", "four");
     
      map.add("4c", "fourC"); // no effect (key already contained)
     
      trace (map.toArray()); // four,four,four,one,two,three
     
      // Replace

      map.replaceFor(3, "three3");
      map.replaceFor("4a", "fourA");
     
      map.replaceFor(1, "one"); // no effect ("one" === "one")
      map.replaceFor(4, "four"); // no effect (key not contained)
     
      trace (map.toArray()); // fourA,four,four,one,two,three3

      // Inspection
     
      trace (map.size); // 6
      trace (map.hasKey(3)); // true
      trace (map.hasKey(4)); // false
      trace (map.itemFor(3)); // three3
      trace (map.itemFor(4)); // undefined
      trace (map.keysToArray()); // 4a,4b,4c,1,2,3

      // Iterator

      var iterator : IIterator = map.iterator();
      trace (iterator is IIterator); // true
      trace (iterator is ICollectionIterator); // true
      trace (iterator is IMapIterator); // true

      while (iterator.hasNext()) {
        trace (iterator.next()); // fourA,four,four,one,two,three3
      }

      // Map iterator

      var mi : IMapIterator = map.iterator() as IMapIterator;
      trace (mi.previousKey + " [" + mi.key + "=" + mi.current + "] " + mi.nextKey);
      while (mi.hasNext()) {
        mi.next();
        trace (mi.previousKey + " [" + mi.key + "=" + mi.current + "] " + mi.nextKey);
      }

      // undefined [undefined=undefined] 4a
      // 4a [4a=fourA] 4b
      // 4b [4b=four] 4c
      // 4c [4c=four] 1
      // 1 [1=one] 2
      // 2 [2=two] 3
      // 3 [3=three3] undefined
     
      // Remove

      map.removeKey(2);
      map.removeKey("4a");     
      map.removeKey(4); // no effect (key not contained)
     
      trace (map.toArray()); // four,four,one,three3

      map.remove("one");     
      map.remove("fourC"); // no effect (item not contained)
     
      trace (map.toArray()); // four,four,three3

      map.removeAll("four");     
      trace (map.toArray()); // three3
    }
  }
}

LinkedMap – Usage

This example shows the specific behaviour of a LinkedMap. The general work with a map is more detailed illustrated in the related Map example. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

LinkedMapExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package {
  import org.as3commons.collections.LinkedMap;
  import org.as3commons.collections.framework.ICollectionIterator;
  import org.as3commons.collections.framework.IIterator;
  import org.as3commons.collections.framework.IMapIterator;
  import org.as3commons.collections.framework.IOrderedMapIterator;
  import flash.display.Sprite;

  public class LinkedMapExample extends Sprite {

    public function LinkedMapExample() {
      var map : LinkedMap = new LinkedMap();
     
      // Add
     
      map.add(4, "four");
      map.add(1, "one");
      map.add(3, "three");
      map.add(5, "five");
      map.add(2, "two");
      trace (map.keysToArray()); // 4,1,3,5,2
      trace (map.toArray()); // four,one,three,five,two
     
      map.addBefore(3, 6, "six");
      map.addAfter(5, 7, "seven");
      trace (map.keysToArray()); // 4,1,6,3,5,7,2
      trace (map.toArray()); // four,one,six,three,five,seven,two

      map.addFirst(8, "eight");
      map.addLast(9, "nine");
      trace (map.keysToArray()); // 8,4,1,6,3,5,7,2,9
      trace (map.toArray()); // eight,four,one,six,three,five,seven,two,nine

      // Inspection
     
      trace (map.first); // eight
      trace (map.last); // nine
     
      // Iterator

      var iterator : IIterator = map.iterator();
      trace (iterator is IIterator); // true
      trace (iterator is ICollectionIterator); // true
      trace (iterator is IMapIterator); // true
      trace (iterator is IOrderedMapIterator); // true
     
      while (iterator.hasNext()) {
        trace (iterator.next()); // eight,four,one,six,three,five,seven,two,nine
      }

      // Remove

      map.removeFirst();
      map.removeFirst();
      map.removeLast();
      map.removeLast();
      trace (map.keysToArray()); // 1,6,3,5,7
      trace (map.toArray()); // one,six,three,five,seven
    }
  }
}

SortedMap – Usage

This example shows the specific behaviour of a SortedMap. The general work with a map is more detailed illustrated in the related Map example. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

SortedMapExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package {
  import org.as3commons.collections.SortedMap;
  import org.as3commons.collections.framework.ICollectionIterator;
  import org.as3commons.collections.framework.IIterator;
  import org.as3commons.collections.framework.IMapIterator;
  import org.as3commons.collections.utils.NumericComparator;
  import flash.display.Sprite;

  public class SortedMapExample extends Sprite {

    public function SortedMapExample() {
     
      var map : SortedMap = new SortedMap(new NumericComparator());
     
      // Add
     
      map.add("threeA", 3);
      map.add("four", 4);
      map.add("one", 1);
      map.add("threeB", 3);
      map.add("five", 5);
      map.add("two", 2);
      map.add("threeC", 3);
      trace (map.keysToArray()); // one,two,threeA,threeB,threeC,four,five
      trace (map.toArray()); // 1,2,3,3,3,4,5
     
      // Replace

      map.replaceFor("one", 4);
      map.replaceFor("threeB", -3);
      trace (map.keysToArray()); // threeB,two,threeA,threeC,four,one,five
      trace (map.toArray()); // -3,2,3,3,4,4,5

      // Inspection
     
      trace (map.first); // -3
      trace (map.last); // 5

      trace (map.hasEqual(3)); // true
      trace (map.hasEqual(6)); // false
      trace (map.equalKeys(3)); // threeA,threeC
     
      trace (map.lesserKey(-4)); // undefined
      trace (map.lesserKey(-3)); // undefined
      trace (map.lesserKey(-2)); // threeB
      trace (map.lesserKey(4)); // threeC
     
      trace (map.higherKey(2)); // threeA
      trace (map.higherKey(3)); // four
      trace (map.higherKey(5)); // undefined
      trace (map.higherKey(6)); // undefined
     
      // Iterator

      var iterator : IIterator = map.iterator();
      trace (iterator is IIterator); // true
      trace (iterator is ICollectionIterator); // true
      trace (iterator is IMapIterator); // true
     
      while (iterator.hasNext()) {
        trace (iterator.next()); // -3,2,3,3,4,4,5
      }

      // Remove

      map.removeFirst();
      map.removeFirst();
      map.removeLast();
      map.removeLast();
      trace (map.keysToArray()); // threeA,threeC,four
      trace (map.toArray()); // 3,3,4
    }
  }
}

LinkedList – Usage

This example shows the specific behaviour of a LinkedList. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

Category: Collection
LinkedListExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package {
  import org.as3commons.collections.LinkedList;
  import org.as3commons.collections.framework.ICollectionIterator;
  import org.as3commons.collections.framework.IIterator;
  import org.as3commons.collections.framework.ILinkedList;
  import org.as3commons.collections.framework.ILinkedListIterator;
  import flash.display.Sprite;

  public class LinkedListExample extends Sprite {

    public function LinkedListExample() {
      var list : ILinkedList = new LinkedList();
     
      // Add
     
      list.add(4);
      list.add(1);
      list.add(3);
      list.add(5);
      list.add(2);
      trace (list.toArray()); // 4,1,3,5,2
     
      list.addFirst(6);
      list.addLast(7);
      trace (list.toArray()); // 6,4,1,3,5,2,7

      // Inspection
     
      trace (list.first); // 6
      trace (list.last); // 7
     
      // Iterator

      var iterator : IIterator = list.iterator();
      trace (iterator is IIterator); // true
      trace (iterator is ICollectionIterator); // true
      trace (iterator is ILinkedListIterator); // true
     
      while (iterator.hasNext()) {
        trace (iterator.next()); // 6,4,1,3,5,2,7
      }

      // Linked list iterator

      var lli : ILinkedListIterator = list.iterator() as ILinkedListIterator;
      trace (lli.previousItem + " [" + lli.current + "] " + lli.nextItem);
      while (lli.hasNext()) {
        lli.next();
        trace (lli.previousItem + " [" + lli.current + "] " + lli.nextItem);
      }

      // undefined [undefined] 6
      // 6 [6] 4
      // 4 [4] 1
      // 1 [1] 3
      // 3 [3] 5
      // 5 [5] 2
      // 2 [2] 7
      // 7 [7] undefined

      // Remove

      list.removeFirst();
      list.removeFirst();
      list.removeLast();
      list.removeLast();
      trace (list.toArray()); // 1,3,5
    }
  }
}

Treap – Usage

This example shows the specific behaviour of a Treap. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

TreapExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package {
  import org.as3commons.collections.Treap;
  import org.as3commons.collections.framework.IBinarySearchTreeIterator;
  import org.as3commons.collections.framework.ICollectionIterator;
  import org.as3commons.collections.framework.IIterator;
  import org.as3commons.collections.utils.NumericComparator;
  import flash.display.Sprite;

  public class TreapExample extends Sprite {

    public function TreapExample() {
      var treap : Treap = new Treap(new NumericComparator());
     
      // Add
     
      treap.add(3);
      treap.add(4);
      treap.add(1);
      treap.add(5);
      treap.add(2);
      treap.add(3); // no effect (equal item already contained)
      trace (treap.toArray()); // 1,2,3,4,5
     
      // Inspection
     
      trace (treap.first); // 1
      trace (treap.last); // 5

      trace (treap.hasEqual(3)); // true
      trace (treap.hasEqual(6)); // false
      trace (treap.equalItem(3)); // 3
     
      trace (treap.lesser(0)); // undefined
      trace (treap.lesser(1)); // undefined
      trace (treap.lesser(2)); // 1
      trace (treap.lesser(8)); // 5
     
      trace (treap.higher(-5)); // 1
      trace (treap.higher(2)); // 3
      trace (treap.higher(5)); // undefined
      trace (treap.higher(8)); // undefined
     
      // Iterator

      var iterator : IIterator = treap.iterator();
      trace (iterator is IIterator); // true
      trace (iterator is ICollectionIterator); // true
      trace (iterator is IBinarySearchTreeIterator); // true
     
      while (iterator.hasNext()) {
        trace (iterator.next()); // 1,2,3,4,5
      }

      // Binary search tree iterator

      var bsti : IBinarySearchTreeIterator = treap.iterator() as IBinarySearchTreeIterator;
      trace (bsti.previousItem + " [" + bsti.current + "] " + bsti.nextItem);
      while (bsti.hasNext()) {
        bsti.next();
        trace (bsti.previousItem + " [" + bsti.current + "] " + bsti.nextItem);
      }

      // undefined [undefined] 1
      // 1 [1] 2
      // 2 [2] 3
      // 3 [3] 4
      // 4 [4] 5
      // 5 [5] undefined

      // Remove

      treap.removeFirst();
      treap.removeLast();
      trace (treap.toArray()); // 2,3,4
    }
  }
}

ArrayListFx – Bindable ArrayList

This example shows the way events dispatched by the ArrayListFx can be listened and evaluated. The ArrayListFx behaviour is identical to that of the ArrayList which is illustrated in the related example.

Related: ArrayList
ArrayListFxExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package {
  import org.as3commons.collections.fx.ArrayListFx;
  import org.as3commons.collections.fx.events.CollectionEvent;
  import org.as3commons.collections.fx.events.ListEvent;
  import org.as3commons.collections.utils.NumericComparator;
  import flash.display.Sprite;

  public class ArrayListFxExample extends Sprite {

    public function ArrayListFxExample() {
      var list : ArrayListFx = new ArrayListFx();
      list.addEventListener(CollectionEvent.COLLECTION_CHANGED, changedHandler);
     
      list.add(1);
      list.addAllAt(1, [2, 2, 4, 4, 5, 5, 6, 6, 8, 8]);
      list.addFirst(10);
      list.addLast(12);

      list.replaceAt(3, 2); // no event (item at 3 is already 2)
      list.replaceAt(6, 2);
      list.replaceAt(7, 2);

      list.removeFirst();
      list.removeLast();
      list.removeAt(2);
      list.removeAt(12); // no event (index > list.size)
      list.removeAllAt(2, 2);

      list.remove(6);
      list.removeAll(2);
      list.removeAll(10); // no event (10 not contained)

      list.reverse();
      list.sort(new NumericComparator());
      list.array = [1, 2, 3, 4, 5];

      list.clear();

      // [1] added at 0                        [1]
      // [2,2,4,4,5,5,6,6,8,8] added at 1      [1 2 2 4 4 5 5 6 6 8 8]
      // [10] added at 0                       [10 1 2 2 4 4 5 5 6 6 8 8]
      // [12] added at 12                      [10 1 2 2 4 4 5 5 6 6 8 8 12]
      // [2] replaced at 6                     [10 1 2 2 4 4 2 5 6 6 8 8 12]
      // [2] replaced at 7                     [10 1 2 2 4 4 2 2 6 6 8 8 12]
      // [10] removed at 0                     [1 2 2 4 4 2 2 6 6 8 8 12]
      // [12] removed at 11                    [1 2 2 4 4 2 2 6 6 8 8]
      // [2] removed at 2                      [1 2 4 4 2 2 6 6 8 8]
      // [4,4] removed at 2                    [1 2 2 2 6 6 8 8]
      // [6] removed at 4                      [1 2 2 2 6 8 8]
      // [2] removed at 1                      [1 2 2 6 8 8]
      // [2] removed at 1                      [1 2 6 8 8]
      // [2] removed at 1                      [1 6 8 8]
      // Reset                                 [8 8 6 1]
      // Reset                                 [1 6 8 8]
      // Reset                                 [1 2 3 4 5]
      // Reset                                 []
    }
   
    private function changedHandler(e : ListEvent) : void {
      var info : String = "";
      var items : *;
     
      switch (e.kind) {
        case CollectionEvent.ITEM_ADDED:
          items = e.numItems == 1 ? e.item : e.items;
          info += "[" + items + "] added at " + e.index;
          break;

        case CollectionEvent.ITEM_REMOVED:
          items = e.numItems == 1 ? e.item : e.items;
          info += "[" + items + "] removed at " + e.index;
          break;

        case CollectionEvent.ITEM_REPLACED:
          info += "[" + e.item + "] replaced at " + e.index;
          break;

        case CollectionEvent.RESET:
          info += ("Reset");
          break;
      }
     
      for (var i : uint = info.length; i < 38; i++) info += " ";
      info += "[" + e.list.toArray().join(" ") + "]";
      trace (info);
    }
  }
}

SortedListFx – Bindable SortedList

This example shows the way events dispatched by the SortedListFx can be listened and evaluated. The SortedListFx behaviour is identical to that of the SortedList which is illustrated in the related example.

Related: SortedList
SortedListFxExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package {
  import org.as3commons.collections.fx.SortedListFx;
  import org.as3commons.collections.fx.events.CollectionEvent;
  import org.as3commons.collections.fx.events.ListEvent;
  import org.as3commons.collections.utils.NumericComparator;
  import flash.display.Sprite;

  public class SortedListFxExample extends Sprite {

    public function SortedListFxExample() {
      var list : SortedListFx = new SortedListFx(new NumericComparator());
      list.addEventListener(CollectionEvent.COLLECTION_CHANGED, changedHandler);
     
      list.array = [4, 2, 3, 1, 2, 6, 8, 4];
      list.add(5);
      list.add(2);
      list.add(2);
      list.add(1);

      list.removeFirst();
      list.removeLast();
      list.removeAt(5);
      list.removeAt(12); // no event (index > list.size)
      list.removeAllAt(4, 2);

      list.remove(6);
      list.removeAll(2);
      list.removeAll(10); // no event (10 not contained)

      list.clear();

      // Reset                   [1 2 2 3 4 4 6 8]
      // [5] added at 6          [1 2 2 3 4 4 5 6 8]
      // [2] added at 3          [1 2 2 2 3 4 4 5 6 8]
      // [2] added at 4          [1 2 2 2 2 3 4 4 5 6 8]
      // [1] added at 1          [1 1 2 2 2 2 3 4 4 5 6 8]
      // [1] removed at 0        [1 2 2 2 2 3 4 4 5 6 8]
      // [8] removed at 10       [1 2 2 2 2 3 4 4 5 6]
      // [3] removed at 5        [1 2 2 2 2 4 4 5 6]
      // [2,4] removed at 4      [1 2 2 2 4 5 6]
      // [6] removed at 6        [1 2 2 2 4 5]
      // [2] removed at 1        [1 2 2 4 5]
      // [2] removed at 1        [1 2 4 5]
      // [2] removed at 1        [1 4 5]
      // Reset                   []
    }
   
    private function changedHandler(e : ListEvent) : void {
      var info : String = "";
      var items : *;
     
      switch (e.kind) {
        case CollectionEvent.ITEM_ADDED:
          items = e.numItems == 1 ? e.item : e.items;
          info += "[" + items + "] added at " + e.index;
          break;

        case CollectionEvent.ITEM_REMOVED:
          items = e.numItems == 1 ? e.item : e.items;
          info += "[" + items + "] removed at " + e.index;
          break;

        case CollectionEvent.ITEM_REPLACED:
          info += "[" + e.item + "] replaced at " + e.index;
          break;

        case CollectionEvent.RESET:
          info += ("Reset");
          break;
      }
     
      for (var i : uint = info.length; i < 24; i++) info += " ";
      info += "[" + e.list.toArray().join(" ") + "]";
      trace (info);
    }
  }
}

SetFx – Bindable Set

This example shows the way events dispatched by the SetFx can be listened and evaluated. The SetFx behaviour is identical to that of the Set which is illustrated in the related example.

Related: Set
SetFxExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package {
  import org.as3commons.collections.fx.SetFx;
  import org.as3commons.collections.fx.events.CollectionEvent;
  import org.as3commons.collections.fx.events.SetEvent;
  import flash.display.Sprite;

  public class SetFxExample extends Sprite {

    public function SetFxExample() {
      var theSet : SetFx = new SetFx();
      theSet.addEventListener(CollectionEvent.COLLECTION_CHANGED, changedHandler);
     
      theSet.add(5);
      theSet.add(2);
      theSet.add(2); // no event (2 already contained)
      theSet.add("one");
      theSet.add("four");
      theSet.add(5); // no event (5 already contained)
      theSet.add(true); // no event (5 already contained)

      theSet.remove(5);
      theSet.remove(6); // no event (6 not contained)
      theSet.remove(true);

      theSet.clear();

      // [5] added                     [5]
      // [2] added                     [2 5]
      // [one] added                   [one 2 5]
      // [four] added                  [four one 2 5]
      // [true] added                  [four one 2 true 5]
      // [5] removed                   [four one 2 true]
      // [true] removed                [four one 2]
      // Reset                         []
    }
   
    private function changedHandler(e : SetEvent) : void {
      var info : String = "";
     
      switch (e.kind) {
        case CollectionEvent.ITEM_ADDED:
          info += "[" + e.item + "] added";
          break;

        case CollectionEvent.ITEM_REMOVED:
          info += "[" + e.item + "] removed";
          break;

        case CollectionEvent.RESET:
          info += ("Reset");
          break;
      }
     
      for (var i : uint = info.length; i < 30; i++) info += " ";
      info += "[" + e.set.toArray().join(" ") + "]";
      trace (info);
    }
  }
}

LinkedSetFx – Bindable LinkedSet

This example shows the way events dispatched by the LinkedSetFx can be listened and evaluated. The LinkedSetFx behaviour is identical to that of the LinkedSet which is illustrated in the related example.

Related: LinkedSet
LinkedSetFxExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package {
  import org.as3commons.collections.framework.ISetIterator;
  import org.as3commons.collections.fx.LinkedSetFx;
  import org.as3commons.collections.fx.events.CollectionEvent;
  import org.as3commons.collections.fx.events.SetEvent;
  import org.as3commons.collections.utils.NumericComparator;
  import flash.display.Sprite;

  public class LinkedSetFxExample extends Sprite {

    public function LinkedSetFxExample() {
      var theSet : LinkedSetFx = new LinkedSetFx();
      theSet.addEventListener(CollectionEvent.COLLECTION_CHANGED, changedHandler);
     
      theSet.add(1);
      theSet.add(3);
      theSet.add(2);
      theSet.add(2); // no event (2 already contained)
     
      theSet.addFirst(4);
      theSet.addLast(5);

      theSet.addBefore(2, 6);
      theSet.addAfter(2, 7);

      theSet.replace(3, 8);
      theSet.replace(1, 9);

      theSet.removeFirst();
      theSet.removeLast();
      theSet.remove(8);
      theSet.remove(6);
      theSet.remove(10); // no event (10 not contained)

      theSet.reverse();
      theSet.sort(new NumericComparator());

      theSet.clear();

      // [1] added                         [1]
      // [3] added after 1                 [1 3]
      // [2] added after 3                 [1 3 2]
      // [4] added before 1                [4 1 3 2]
      // [5] added after 2                 [4 1 3 2 5]
      // [6] added after 3 before 2        [4 1 3 6 2 5]
      // [7] added after 2 before 5        [4 1 3 6 2 7 5]
      // [8] replaced after 1 before 6     [4 1 8 6 2 7 5]
      // [9] replaced after 4 before 8     [4 9 8 6 2 7 5]
      // [4] removed before 9              [9 8 6 2 7 5]
      // [5] removed after 7               [9 8 6 2 7]
      // [8] removed after 9 before 6      [9 6 2 7]
      // [6] removed after 9 before 2      [9 2 7]
      // Reset                             [7 2 9]
      // Reset                             [2 7 9]
      // Reset                             []
    }

    private function changedHandler(e : SetEvent) : void {
      var info : String = "";
      var iterator : ISetIterator;
     
      switch (e.kind) {
        case CollectionEvent.ITEM_ADDED:
          info += "[" + e.item + "] added";

          iterator = e.iterator() as ISetIterator;
          if (iterator.previousItem !== undefined) info += " after " + iterator.previousItem;
          iterator.next();
          if (iterator.nextItem !== undefined) info += " before " + iterator.nextItem;
          break;

        case CollectionEvent.ITEM_REMOVED:
          info += "[" + e.item + "] removed";

          iterator = e.iterator() as ISetIterator;
          if (iterator.previousItem !== undefined) info += " after " + iterator.previousItem;
          if (iterator.nextItem !== undefined) info += " before " + iterator.nextItem;
          break;

        case CollectionEvent.ITEM_REPLACED:
          info += "[" + e.item + "] replaced";

          iterator = e.iterator() as ISetIterator;
          if (iterator.previousItem !== undefined) info += " after " + iterator.previousItem;
          iterator.next();
          if (iterator.nextItem !== undefined) info += " before " + iterator.nextItem;
          break;

        case CollectionEvent.RESET:
          info += ("Reset");
          break;
      }
     
      for (var i : uint = info.length; i < 34; i++) info += " ";
      info += "[" + e.set.toArray().join(" ") + "]";
      trace (info);
    }
  }
}

SortedSetFx – Bindable SortedSet

This example shows the way events dispatched by the SortedSetFx can be listened and evaluated. The SortedSetFx behaviour is identical to that of the SortedSet which is illustrated in the related example.

Related: SortedSet
SortedSetFxExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package {
  import org.as3commons.collections.framework.ISetIterator;
  import org.as3commons.collections.fx.SortedSetFx;
  import org.as3commons.collections.fx.events.CollectionEvent;
  import org.as3commons.collections.fx.events.SetEvent;
  import org.as3commons.collections.utils.NumericComparator;
  import flash.display.Sprite;

  public class SortedSetFxExample extends Sprite {

    public function SortedSetFxExample() {
      var theSet : SortedSetFx = new SortedSetFx(new NumericComparator());
      theSet.addEventListener(CollectionEvent.COLLECTION_CHANGED, changedHandler);
     
      theSet.add(5);
      theSet.add(2);
      theSet.add(2); // no event (2 already contained)
      theSet.add(1);
      theSet.add(4);
      theSet.add(5); // no event (5 already contained)

      theSet.removeFirst();
      theSet.removeLast();
      theSet.remove(6); // no event (6 not contained)

      theSet.clear();

      // [5] added                     [5]
      // [2] added before 5            [2 5]
      // [1] added before 2            [1 2 5]
      // [4] added after 2 before 5    [1 2 4 5]
      // [1] removed before 2          [2 4 5]
      // [5] removed after 4           [2 4]
      // Reset                         []
    }
   
    private function changedHandler(e : SetEvent) : void {
      var info : String = "";
      var iterator : ISetIterator;
     
      switch (e.kind) {
        case CollectionEvent.ITEM_ADDED:
          info += "[" + e.item + "] added";

          iterator = e.iterator() as ISetIterator;
          if (iterator.previousItem !== undefined) info += " after " + iterator.previousItem;
          iterator.next();
          if (iterator.nextItem !== undefined) info += " before " + iterator.nextItem;
          break;

        case CollectionEvent.ITEM_REMOVED:
          info += "[" + e.item + "] removed";

          iterator = e.iterator() as ISetIterator;
          if (iterator.previousItem !== undefined) info += " after " + iterator.previousItem;
          if (iterator.nextItem !== undefined) info += " before " + iterator.nextItem;
          break;

        case CollectionEvent.RESET:
          info += ("Reset");
          break;
      }
     
      for (var i : uint = info.length; i < 30; i++) info += " ";
      info += "[" + e.set.toArray().join(" ") + "]";
      trace (info);
    }
  }
}

MapFx – Bindable Map

This example shows the way events dispatched by the MapFx can be listened and evaluated. The MapFx behaviour is identical to that of the Map which is illustrated in the related example.

Related: Map
MapFxExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package {
  import org.as3commons.collections.fx.MapFx;
  import org.as3commons.collections.fx.events.CollectionEvent;
  import org.as3commons.collections.fx.events.MapEvent;
  import flash.display.Sprite;

  public class MapFxExample extends Sprite {

    public function MapFxExample() {
      var map : MapFx = new MapFx();
      map.addEventListener(CollectionEvent.COLLECTION_CHANGED, changedHandler);
     
      map.add("five", 5);
      map.add("two", 2);
      map.add("threeA", "three");
      map.add("two", 2); // no event (two already contained)
      map.add("four", 4);
      map.add("threeB", "three");
      map.add("one", 1);
      map.add("threeC", "three");

      map.replaceFor("four", -4);
      map.replaceFor("one", 1); // no event (1 === 1)

      map.removeKey("one");
      map.remove(6); // no event (6 not contained)
      map.removeAll("three");

      map.clear();

      // [five=5] added                    [5]
      // [two=2] added                     [2 5]
      // [threeA=three] added              [2 5 three]
      // [four=4] added                    [2 4 5 three]
      // [threeB=three] added              [2 4 5 three three]
      // [one=1] added                     [2 4 1 5 three three]
      // [threeC=three] added              [2 three 5 three 4 1 three]
      // [four=-4] replaced                [2 three 5 three -4 1 three]
      // [one=1] removed                   [2 three 5 three -4 three]
      // [threeC=three] removed            [2 5 three -4 three]
      // [threeA=three] removed            [2 5 -4 three]
      // [threeB=three] removed            [2 5 -4]
      // Reset                             []
    }
   
    private function changedHandler(e : MapEvent) : void {
      var info : String = "";
     
      switch (e.kind) {
        case CollectionEvent.ITEM_ADDED:
          info += "[" + e.key + "=" + e.item + "] added";
          break;

        case CollectionEvent.ITEM_REMOVED:
          info += "[" + e.key + "=" + e.item + "] removed";
          break;

        case CollectionEvent.ITEM_REPLACED:
          info += "[" + e.key + "=" + e.item + "] replaced";
          break;

        case CollectionEvent.RESET:
          info += ("Reset");
          break;
      }
     
      for (var i : uint = info.length; i < 34; i++) info += " ";
      info += "[" + e.map.toArray().join(" ") + "]";
      trace (info);
    }
  }
}

LinkedMapFx – Bindable LinkedMap

This example shows the way events dispatched by the LinkedMapFx can be listened and evaluated. The LinkedMapFx behaviour is identical to that of the LinkedMap which is illustrated in the related example.

Related: LinkedMap
LinkedMapFxExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package {
  import org.as3commons.collections.framework.IMapIterator;
  import org.as3commons.collections.fx.LinkedMapFx;
  import org.as3commons.collections.fx.events.CollectionEvent;
  import org.as3commons.collections.fx.events.MapEvent;
  import org.as3commons.collections.utils.NumericComparator;
  import flash.display.Sprite;

  public class LinkedMapFxExample extends Sprite {

    public function LinkedMapFxExample() {
      var map : LinkedMapFx = new LinkedMapFx();
      map.addEventListener(CollectionEvent.COLLECTION_CHANGED, changedHandler);
     
      map.add("fourA", 4);
      map.add("one", 1);
      map.add("threeA", 3);
      map.add("two", 2);
      map.add("two", 2); // no event (two already contained)
     
      map.addFirst("threeD", 3);
      map.addLast("fourB", 4);

      map.addBefore("two", "threeB", 3);
      map.addAfter("two", "threeC", 3);

      map.replaceFor("two", 22);
      map.replaceFor("one", 11);
      map.replaceFor("one", 11); // no event (11 == 11)

      map.removeFirst();
      map.removeLast();
      map.remove(6); // no event (6 not contained)
      map.removeAll(3);

      map.reverse();
      map.sort(new NumericComparator());

      map.clear();

      // [4] added                         [4]
      // [1] added after 4                 [4 1]
      // [3] added after 1                 [4 1 3]
      // [2] added after 3                 [4 1 3 2]
      // [3] added before 4                [3 4 1 3 2]
      // [4] added after 2                 [3 4 1 3 2 4]
      // [3] added after 3 before 2        [3 4 1 3 3 2 4]
      // [3] added after 2 before 4        [3 4 1 3 3 2 3 4]
      // [22] replaced after 3 before 3    [3 4 1 3 3 22 3 4]
      // [11] replaced after 4 before 3    [3 4 11 3 3 22 3 4]
      // [3] removed before 4              [4 11 3 3 22 3 4]
      // [4] removed after 3               [4 11 3 3 22 3]
      // [3] removed after 11 before 3     [4 11 3 22 3]
      // [3] removed after 11 before 22    [4 11 22 3]
      // [3] removed after 22              [4 11 22]
      // Reset                             [22 11 4]
      // Reset                             [4 11 22]
      // Reset                             []
    }

    private function changedHandler(e : MapEvent) : void {
      var info : String = "";
      var iterator : IMapIterator;
     
      switch (e.kind) {
        case CollectionEvent.ITEM_ADDED:
          info += "[" + e.item + "] added";

          iterator = e.iterator() as IMapIterator;
          if (iterator.previousKey !== undefined) info += " after " + e.map.itemFor(iterator.previousKey);
          iterator.next();
          if (iterator.nextKey !== undefined) info += " before " + e.map.itemFor(iterator.nextKey);
          break;

        case CollectionEvent.ITEM_REMOVED:
          info += "[" + e.item + "] removed";

          iterator = e.iterator() as IMapIterator;
          if (iterator.previousKey !== undefined) info += " after " + e.map.itemFor(iterator.previousKey);
          if (iterator.nextKey !== undefined) info += " before " + e.map.itemFor(iterator.nextKey);
          break;

        case CollectionEvent.ITEM_REPLACED:
          info += "[" + e.item + "] replaced";

          iterator = e.iterator() as IMapIterator;
          if (iterator.previousKey !== undefined) info += " after " + e.map.itemFor(iterator.previousKey);
          iterator.next();
          if (iterator.nextKey !== undefined) info += " before " + e.map.itemFor(iterator.nextKey);
          break;

        case CollectionEvent.RESET:
          info += ("Reset");
          break;
      }
     
      for (var i : uint = info.length; i < 34; i++) info += " ";
      info += "[" + e.map.toArray().join(" ") + "]";
      trace (info);
    }
  }
}

SortedMapFx – Bindable SortedMap

This example shows the way events dispatched by the SortedMapFx can be listened and evaluated. The SortedMapFx behaviour is identical to that of the SortedMap which is illustrated in the related example.

Related: SortedMap
SortedMapFxExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package {
  import org.as3commons.collections.framework.IMapIterator;
  import org.as3commons.collections.fx.SortedMapFx;
  import org.as3commons.collections.fx.events.CollectionEvent;
  import org.as3commons.collections.fx.events.MapEvent;
  import org.as3commons.collections.utils.NumericComparator;
  import flash.display.Sprite;

  public class SortedMapFxExample extends Sprite {

    public function SortedMapFxExample() {
      var map : SortedMapFx = new SortedMapFx(new NumericComparator());
      map.addEventListener(CollectionEvent.COLLECTION_CHANGED, changedHandler);
     
      map.add("five", 5);
      map.add("two", 2);
      map.add("threeA", 3);
      map.add("two", 2); // no event (two already contained)
      map.add("four", 4);
      map.add("threeB", 3);
      map.add("one", 1);
      map.add("threeC", 3);

      map.replaceFor("four", -4);
      map.replaceFor("one", 1); // no event (1 === 1)

      map.removeKey("one");
      map.removeFirst();
      map.removeLast();
      map.remove(6); // no event (6 not contained)
      map.removeAll(3);

      map.clear();

      // [5] added                         [5]
      // [2] added before 5                [2 5]
      // [3] added after 2 before 5        [2 3 5]
      // [4] added after 3 before 5        [2 3 4 5]
      // [3] added after 3 before 4        [2 3 3 4 5]
      // [1] added before 2                [1 2 3 3 4 5]
      // [3] added after 3 before 4        [1 2 3 3 3 4 5]
      // [4] removed after 3 before 5      [1 2 3 3 3 5]
      // [-4] added before 1               [-4 1 2 3 3 3 5]
      // [1] removed after -4 before 2     [-4 2 3 3 3 5]
      // [-4] removed before 2             [2 3 3 3 5]
      // [5] removed after 3               [2 3 3 3]
      // [3] removed after 2 before 3      [2 3 3]
      // [3] removed after 2 before 3      [2 3]
      // [3] removed after 2               [2]
      // Reset                             []
    }
   
    private function changedHandler(e : MapEvent) : void {
      var info : String = "";
      var iterator : IMapIterator;
     
      switch (e.kind) {
        case CollectionEvent.ITEM_ADDED:
          info += "[" + e.item + "] added";

          iterator = e.iterator() as IMapIterator;
          if (iterator.previousKey !== undefined) info += " after " + e.map.itemFor(iterator.previousKey);
          iterator.next();
          if (iterator.nextKey !== undefined) info += " before " + e.map.itemFor(iterator.nextKey);
          break;

        case CollectionEvent.ITEM_REMOVED:
          info += "[" + e.item + "] removed";

          iterator = e.iterator() as IMapIterator;
          if (iterator.previousKey !== undefined) info += " after " + e.map.itemFor(iterator.previousKey);
          if (iterator.nextKey !== undefined) info += " before " + e.map.itemFor(iterator.nextKey);
          break;

        case CollectionEvent.RESET:
          info += ("Reset");
          break;
      }
     
      for (var i : uint = info.length; i < 34; i++) info += " ";
      info += "[" + e.map.toArray().join(" ") + "]";
      trace (info);
    }
  }
}

CollectionEvent Polymorphy – Type Casting

The event dispatched by a bindable collection is basically of type CollectionEvent and should be downcasted in order to provide the collection specific functionality. This example gives two examples how such events may be downcasted.

CollectionEventDowncastExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package {
  import org.as3commons.collections.fx.ArrayListFx;
  import org.as3commons.collections.fx.events.CollectionEvent;
  import org.as3commons.collections.fx.events.ListEvent;
  import flash.display.Sprite;

  public class CollectionEventDowncastExample extends Sprite {

    public function CollectionEventDowncastExample() {
      var list : ArrayListFx = new ArrayListFx();
      list.addEventListener(CollectionEvent.COLLECTION_CHANGED, changedHandler);
      list.addEventListener(CollectionEvent.COLLECTION_CHANGED, changedHandler2);
      list.add(1);
    }
   
    // possibility 1
    private function changedHandler(e : CollectionEvent) : void {
      var listEvent : ListEvent = e as ListEvent;
      trace (listEvent.index, listEvent.item); // 0 1
    }

    // possibility 2
    private function changedHandler2(e : ListEvent) : void {
      trace (e.index, e.item); // 0 1
    }
  }
}

CollectionEvent Iterator – Locating changes

The iterator returned by the iterator() method of each CollectionEvent points always to the position where the change has been occurred and thus enables a valid tracking of the state of a collection. The example shows this iterator in action.

CollectionEventIteratorExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package {
  import org.as3commons.collections.framework.IListIterator;
  import org.as3commons.collections.fx.ArrayListFx;
  import org.as3commons.collections.fx.events.CollectionEvent;
  import flash.display.Sprite;

  public class CollectionEventIteratorExample extends Sprite {

    public function CollectionEventIteratorExample() {
      var list : ArrayListFx = new ArrayListFx();
      list.array = [1, 2, 4, 5];
      list.addEventListener(CollectionEvent.COLLECTION_CHANGED, changedHandler);

      list.addAt(2, 3); // 1, 2, 3, 4, 5
      list.replaceAt(3, 6); // 1, 2, 3, 6, 5
      list.removeAt(4); // 1, 2, 3, 6
      list.reverse(); // 6, 3, 2, 1
    }
   
    private function changedHandler(event : CollectionEvent) : void {
      var iterator : IListIterator = event.iterator() as IListIterator;
     
      switch (event.kind) {
        case CollectionEvent.ITEM_ADDED: // 1 2 ^ 3 4 5
          trace (iterator.previousIndex); // 1
          trace (iterator.nextIndex); // 2
          trace (iterator.next()); // 3
          break;

        case CollectionEvent.ITEM_REPLACED: // 1 2 3 ^ 6 5
          trace (iterator.previousIndex); // 2
          trace (iterator.nextIndex); // 3
          trace (iterator.next()); // 6
          break;

        case CollectionEvent.ITEM_REMOVED: // 1 2 3 6 ^
          trace (iterator.previousIndex); // 3
          trace (iterator.nextIndex); // -1
          trace (iterator.next()); // undefined
          break;

        case CollectionEvent.RESET:
          trace (iterator); // null
          break;
      }
    }
  }
}

CollectionEvent Iterator Polymorphy – Type Casting

The iterator returned by the iterator() method of a CollectionEvent is basically of type ICollectionIterator. Depending on the source collection, this iterator may be downcasted to a more specific type which provides more functionality. The example shows how such an iterator may be downcasted.

CollectionEventIteratorDownCastExample.as: Source raw, Source highlighted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package {
  import org.as3commons.collections.framework.ICollectionIterator;
  import org.as3commons.collections.framework.IIterator;
  import org.as3commons.collections.framework.IListIterator;
  import org.as3commons.collections.framework.IOrderedListIterator;
  import org.as3commons.collections.fx.ArrayListFx;
  import org.as3commons.collections.fx.events.CollectionEvent;
  import flash.display.Sprite;

  public class CollectionEventIteratorDownCastExample extends Sprite {

    public function CollectionEventIteratorDownCastExample() {
      var list : ArrayListFx = new ArrayListFx();
      list.addEventListener(CollectionEvent.COLLECTION_CHANGED, changedHandler);
      list.add(1);
    }
   
    private function changedHandler(event : CollectionEvent) : void {
      var i : IIterator = event.iterator() as IIterator;
     
      trace ("i", i is IIterator); // true
      trace ("ci", i is ICollectionIterator); // true
      trace ("li", i is IListIterator); // true
      trace ("ali", i is IOrderedListIterator); // true
     
      var ci : ICollectionIterator = event.iterator() as ICollectionIterator;

      trace ("i", ci is IIterator); // true
      trace ("ci", ci is ICollectionIterator); // true
      trace ("li", ci is IListIterator); // true
      trace ("ali", ci is IOrderedListIterator); // true

      var li : IListIterator = event.iterator() as IListIterator;

      trace ("i", li is IIterator); // true
      trace ("ci", li is ICollectionIterator); // true
      trace ("li", li is IListIterator); // true
      trace ("ali", li is IOrderedListIterator); // true

      var ali : IOrderedListIterator = event.iterator() as IOrderedListIterator;

      trace ("i", ali is IIterator); // true
      trace ("ci", ali is ICollectionIterator); // true
      trace ("li", ali is IListIterator); // true
      trace ("ali", ali is IOrderedListIterator); // true
    }
  }
}

All Examples

Collections

ArrayList – Usage

The example shows the usage of the methods of the ArrayList collection.

SortedList – Usage

This example shows the specific behaviour of a SortedList. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

Set – Usage

This example shows the specific behaviour of a Set. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

Category: Set, Collection

LinkedSet – Usage

This example shows the specific behaviour of a LinkedSet. The general work with a set is more detailed illustrated in the related Set example. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

SortedSet – Usage

This example shows the specific behaviour of a SortedSet. The general work with a set is more detailed illustrated in the related Set example. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

Map – Usage

This example shows the specific behaviour of a Map. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

Category: Map, Collection

LinkedMap – Usage

This example shows the specific behaviour of a LinkedMap. The general work with a map is more detailed illustrated in the related Map example. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

SortedMap – Usage

This example shows the specific behaviour of a SortedMap. The general work with a map is more detailed illustrated in the related Map example. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

LinkedList – Usage

This example shows the specific behaviour of a LinkedList. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

Category: Collection

Treap – Usage

This example shows the specific behaviour of a Treap. The general work with collections and iterators is more detailed illustrated in the related ArrayList and ArrayListIterator examples.

Iterators

CollectionIterator – Traversing and Modifying a Collection

Each collection is equipped with an iterator of type ICollectionIterator that offers rich navigation (start, end, previous) and modification (remove) possibilities. The example shows the usage of the methods of a CollectionIterator.

Category: Iterator

Iterator Polymorphy – Type Casting

The iterator returned by the iterator() method of a collection is always an instance of ICollectionIterator which extends the IIterator interface. Depending on the actual collections, the returned iterator may be even more specific. This example shows how an iterator may be downcasted to offer more specific functionality.

Category: Iterator

ArrayListIterator – Usage

The example shows the usage of the methods of an ArrayListIterator which extends the ICollectionIterator interface with index based navigation and modification operations.

Category: Iterator

Bindable Collections

ArrayListFx – Bindable ArrayList

This example shows the way events dispatched by the ArrayListFx can be listened and evaluated. The ArrayListFx behaviour is identical to that of the ArrayList which is illustrated in the related example.

Related: ArrayList

SortedListFx – Bindable SortedList

This example shows the way events dispatched by the SortedListFx can be listened and evaluated. The SortedListFx behaviour is identical to that of the SortedList which is illustrated in the related example.

Related: SortedList

SetFx – Bindable Set

This example shows the way events dispatched by the SetFx can be listened and evaluated. The SetFx behaviour is identical to that of the Set which is illustrated in the related example.

Related: Set

LinkedSetFx – Bindable LinkedSet

This example shows the way events dispatched by the LinkedSetFx can be listened and evaluated. The LinkedSetFx behaviour is identical to that of the LinkedSet which is illustrated in the related example.

Related: LinkedSet

SortedSetFx – Bindable SortedSet

This example shows the way events dispatched by the SortedSetFx can be listened and evaluated. The SortedSetFx behaviour is identical to that of the SortedSet which is illustrated in the related example.

Related: SortedSet

MapFx – Bindable Map

This example shows the way events dispatched by the MapFx can be listened and evaluated. The MapFx behaviour is identical to that of the Map which is illustrated in the related example.

Related: Map

LinkedMapFx – Bindable LinkedMap

This example shows the way events dispatched by the LinkedMapFx can be listened and evaluated. The LinkedMapFx behaviour is identical to that of the LinkedMap which is illustrated in the related example.

Related: LinkedMap

SortedMapFx – Bindable SortedMap

This example shows the way events dispatched by the SortedMapFx can be listened and evaluated. The SortedMapFx behaviour is identical to that of the SortedMap which is illustrated in the related example.

Related: SortedMap

Collection Events

CollectionEvent Polymorphy – Type Casting

The event dispatched by a bindable collection is basically of type CollectionEvent and should be downcasted in order to provide the collection specific functionality. This example gives two examples how such events may be downcasted.

CollectionEvent Iterator – Locating changes

The iterator returned by the iterator() method of each CollectionEvent points always to the position where the change has been occurred and thus enables a valid tracking of the state of a collection. The example shows this iterator in action.

CollectionEvent Iterator Polymorphy – Type Casting

The iterator returned by the iterator() method of a CollectionEvent is basically of type ICollectionIterator. Depending on the source collection, this iterator may be downcasted to a more specific type which provides more functionality. The example shows how such an iterator may be downcasted.

Special Iterators

FilterIterator – Filtering a Collection

The FilterIterator class is useful to filter the contents of any iterable data container. This example shows the setup of a simple filter function.

CollectionFilterIterator – Filtering a Collection deluxe

This is a filter iterator with extended navigation (start, end, previous) and modification (remove) possibilities. This example shows the setup of a simple filter function and the usage of the CollectionFilterIterator.

RecursiveIterator – Traversing a complex structure

The RecursiveIterator traverses any complex structure when the items (or nodes) in this structure are again iterable data container. The example shows the basic setup a complex data structure and the usage of the RecursiveIterator class.

RecursiveFilterIterator – Traversing and filtering a complex structure

This is a recursive iterator that enables filtering of parent or child nodes. The example shows the basic setup a complex data structure and illustrates how children or parent nodes can be filtered.

Sorted Collections

Implementing a Comparator

A comparator must be specified to setup a sorted collection or a sort operation. This example shows a comparator that sorts numbers after their parity and then their value.

Tools and Utils

Create a nested collection

Creating a nested collection is easy using the util classes.

Category: Tools and Utils

Fast populate a collection

You may use variable argument lists to add multiple items at once.

Category: Tools and Utils

Copy a list

Copy items from a list to another. It is possible to customize the copy process using filters.

Clone a list

Clone an entire list. It is possible to customize the clone process using filters.

Fast populate a list

Use the Lists.addFrom…() methods to easily set up a list collection.

Copy a set

Copy items from a set to another. It is possible to customize the copy process using filters.

Category: Tools and Utils, Set

Clone a set

Clone an entire set. It is possible to customize the clone process using filters.

Category: Tools and Utils, Set

Fast populate a set

Use the Sets.addFrom…() methods to easily set up a set collection.

Category: Tools and Utils, Set

Copy a map

Copy items from a map to another. It is possible to customize the copy process using filters.

Category: Tools and Utils, Map

Clone a map

Clone an entire map. It is possible to customize the clone process using filters.

Category: Tools and Utils, Map

Fast populate a map

Use the Maps.addFrom…() methods to easily set up a map collection.

Category: Tools and Utils, Map

Dump a Collection

The example show the basic setup of a complex data structure and the application of the dumpAsString() method.

Category: Tools and Utils


2 Comments

  1. Janek

    13. August 2012

    Mislead by the bindable word i see i cannot use for example ArrayListFx to fetch spark List dataProvider ? What’s the point of having them versus your regular ArrayList ? eventually one would want to display something in components. Should i use mx.collections.ArrayList instead, and if so, what is the purpose of ArrayListFx and the rest of bindable collections?

    Please explain, thanks in advance

  2. Paolo

    30. September 2013

    I’ve same question of Janek? What does it means “bindable”? it’s about the collection events only?

Leave a Comment

You have a question or have experienced an issue? Please post it in the forum: http://sibirjak.tenderapp.com/ in order to make the discussion available at a more central place.