3 Replies Latest reply on Feb 19, 2011 7:15 AM by Michael J Roberts

    Finding the difference between 2 ArrayCollections (like java?)

    WebTrauma Level 1

      Hi,

       

      In java you can do a 'diff' between two Lists like this:  myList_new.removeAll(myList_old);  This removes from myList_new all the items identical in myList_old

       

      Is there something similar in Actionscript or do I have to use brute force (loop thru both ArrayCollections and compare) ? My guess is that brute force is required since actionscript doesn't have generics but I'd love to be proved wrong !!!

       

      thanks!

        • 1. Re: Finding the difference between 2 ArrayCollections (like java?)
          Michael J Roberts Level 2

          Unfortunately, there is no out-of-the-box method that will do this for you.  However, here is some code that should accomplish what you need:

          <Taken from: http://stackoverflow.com/questions/1620202/flex-intersecting-2-array-collections>

           

          public function minus(a:ArrayCollection, b:ArrayCollection):ArrayCollection {
              var result:ArrayCollection = new ArrayCollection()
          
              for each (i in a) {
                  if (!b.contains(i)) {
                      result.addItem(i)
                  }
              }
          
              return result
          }
          
          1 person found this helpful
          • 2. Re: Finding the difference between 2 ArrayCollections (like java?)
            WebTrauma Level 1

            Thanks.

             

            I'll try this but am not sure it will work since I have arrayCollections of complex objects, not just single values of string, int, etc.

            • 3. Re: Finding the difference between 2 ArrayCollections (like java?)
              Michael J Roberts Level 2

              If your objects have a unique identifier on them you could do something like below.


              Notice I use value.id as an assumption that "id" is the name of the identifier.  If you have a differently named identifer, you would need to change "id" to that name.

               

              If there is no unique identifier on your objects, I think you'll just have to loop through the Collections and compare the attributes manually.

               

              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;
              }