I have recently been working on a project where I have needed to place objects into an associative array and then sort the keys to ensure that all the objects are in the correct order. I accomplished this by using a keys Array where I kept all of my associative array keys and then I would sort it whenever I added new mapping entries to my associative array. But it was such a pain to remember to do this every time and it started to bloat my code, so I decided to bite the bullet and implement java.util.TreeMap in Actionscript to solve this problem.

Java.util.TreeMap ensures that the object mappings that it contains are in a particular order. The ordering mechanism is ultimately up to you. You can use either the default natural ordering, or supply a Comparator function to tell TreeMap exactly how it should sort its mappings. I decided to make a similar class that not only ensures order, but includes helper functions to make routine tasks easy. Instead of Java’s Comparator class, my TreeMap has a sortCompareFunction property where you can pass it a custom sort function, or you could just use the default sort function that I wrote.

While doing this, I created a base IMap interface which is comparable to the java.util.Map interface and I also went and created an implementation of java.util.HashMap class as well. Although associative arrays can technically do everything that my HashMap can do, I thought it might be useful for those Actionscript converts coming from a Java background who want to see how an underlying associative array is manipulated with a HashMap.

Get the source here.

Here is a summary of all of the functions and properties in TreeMap.

PROPERTIES

// All the values in TreeMap in their correct order
values:Array

// All the keys in TreeMap in their correct order
keys:Array

// The number of mappings in TreeMap
length:Number

// A custom sort function determines the ordering of mappings. If
// this is null, a default sort function is used.
sortCompareFunction:Function

A deep copy of the keys in reverse order
descendingKeys:Array

A deep copy of the TreeMap is reverse order
descendingMap:IMap

FUNCTIONS

// Makes a deep-copy of the map.
clone(map:IMap):IMap

// Determines if the map contains a certain key.
containsKey(key:Object):Boolean

// Determines if the map contains a certain value.
containsValue(val:Object):Boolean

// Returns the value associated with the key argument.
getValue(key:Object):*

// Determines if the TreeMap is empty
isEmpty():Boolean

// Puts a new mapping into the TreeMap
put(key:Object, value:Object):void

// Puts all the mappings contains in an IMap into this TreeMap
putAll(map:IMap):void

// Removes a mapping from the TreeMap
remove(key:Object):void

// Returns the next mapping in the TreeMap
nextEntry(key:Object):MapEntry

// Returns the next key in the TreeMap
nextKey(key:Object):*

// Determines if there is another mapping after the given key
hasNext(key:Object):Boolean

// Returns the first mapping in the TreeMap
firstEntry():MapEntry

// Returns the first key in the TreeMap
firstKey():*

// Returns the last mapping in the TreeMap
lastEntry():MapEntry

// Returns the last key in the TreeMap
lastKey():*

// Returns the mapping just before the given key
previousEntry(key:Object):MapEntry

// Returns the key just before the given key
previousKey(key:Object):*

// Determines if there is a mapping prior to the given key
hasPrevious(key:Object):*

// Returns the last mapping from the TreeMap and removes it.
pop():MapEntry

// Returns the first mapping from the TreeMap and removes it.
shift():MapEntry

// Returns a new TreeMap with values spanning from the beginning of
// the TreeMap to the given key (or including the given key if inclusive)
headMap(key:Object, inclusive:Boolean = false):IMap

// Returns a new TreeMap with values spanning from the end of the
// TreeMap to the given key (or including the given key if inclusive)
tailMap(key:Object, inclusive:Boolean = false):IMap

// Returns a new TreeMap with values spanning from fromKey to
// toKey (fromInclusive includes fromKey and toInclusive includes
// toKey).
subMap(fromKey:Object, toKey:Object,
            fromInclusive:Boolean = false,
            toInclusive:Boolean = false):IMap

Tags: , , , , , , , ,

3 Responses


  1. Antoni Jakubiak on 23 Apr 2009

    Thanks for Your code. Please provide license information.

  2. Nate on 23 Apr 2009

    Feel free to use any bits of code on my site without limitation unless I explicitly make it known otherwise. Thanks!

  3. Rose on 24 Mar 2011

    Great assembly of the java.util.map :) But there is a little error in HashMap.as:

    The functions putAll(…) and put(…) need to check if the map already contains the key and overwrite it.

    I solved it this way:
    if(containsKey(key)){ remove(key); }


Leave your comment


ray ban wayfarer ebay ray ban aviators
bag borrow or steal
Buy shoes online india
replica handbags