Rambling Developer

Recent Posts


Archives


Flex Java removeAll() Implementation

adminadmin

Just created a implementation of the Java removeAll() method for Flex ArrayCollections.  Here it is:


public function removeAll(collection1:ArrayCollection,
                    collection2:ArrayCollection):ArrayCollection{
  var dictionary : Dictionary = new Dictionary(true);
  var value : Object;
  var i:Number
  //Loop through first collection and put objects in dictionary
  for(i = 0; i < collection1.length; i++){
    value = collection1.getItemAt(i);
    dictionary[value.id] = value;
  }
  //Loop through second collection and remove objects in dictionary
  for(i = 0; i < collection2.length; i++){
    value = collection2.getItemAt(i);
    if(dictionary[value.id] != null){
      delete dictionary[value.id];
    }
  }
                     
  var unique:ArrayCollection = new ArrayCollection();
  //Loop through dictionary and put remaining value in collection
  for(var prop:String in dictionary){
    unique.addItem(dictionary[prop]);
  }
  dictionary = null;
  return unique;
}

Here's a link to the Adobe Forum post where I created this snippet: http://forums.adobe.com/thread/793088