8 Replies Latest reply: Dec 6, 2006 9:08 AM by 800322 RSS

    comparison of 2 arraylist

    807607
      Hello all, i would like to have some help on comparing 2 arraylist for my program.
      So i have an arrayList which i call listeA for exemple that is fill with some process object.
      I would like to compare this arrayList with another arrayList, listeB for the exemple that is provided at some regular interval.
      I would like first to add processes from listeB that are not in listeA and also remove processes from listeA that are not in listeB.
      Thanks for your help.
        • 1. Re: comparison of 2 arraylist
          800282
          Thanks for your help.
          With what? You only posted the problem description. You need to ask a (specific) question.
          • 2. Re: comparison of 2 arraylist
            800322
            I would like first to add processes from listeB that
            are not in listeA
            use a Set
            and also remove processes from
            listeA that are not in listeB.
            use Collection.retain()
            • 3. Re: comparison of 2 arraylist
              807607
              Hi prometheuzz,
              My problem is that i dont know how to do it exactly for adding something from one list and removing things from another. It's more a logical or algorithm problem that i have than a problem with java language, even if there might be some mistakes also. But i hope someone can help me. Here is my code :
              if(listeA != null)
                       {
                            
                            for(Iterator itListA = listeA.iterator(); itListA.hasNext();)
                            {
                                 
                                 if(itListA.next() instanceof TestObject)
                                 {
                                      TestObject oldProcess = (TestObject)itListA.next();
                                      String nameOldProcess = oldProcess.getNom();
                                      
                                      for(Iterator itListB = listeB.iterator(); itListB.hasNext();)
                                    {
                                         if(itListB.next() instanceof TestObject)
                                         {
                                              TestObject newProcess = (TestObject)itListB.next();
                                              String nameNewProcess = newProcess.getNom();
                                              if(!nameNewProcess.equals(nameOldProcess))
                                              {
                                                   listeA.add(newProcess);
                                              }
                                         }
                                    }
                                      
                            }
                       
              }
              for the first part that is adding a process to listeA if its in listeB. Then now what i would like to do is removing process from listeA if it is not in listeB without having to do some nested for again, i d like to add and remove within the same for block if possible.
              I hope that i m clear :-/
              Thanks in advance.
              • 4. Re: comparison of 2 arraylist
                807607
                So you want listA to become listB, right
                listA = new ArrayList(listB);
                DONE
                • 5. Re: comparison of 2 arraylist
                  807607
                  but same processes can be in both list. And my processes run in thread so it can be a problem to replace the whole list i think. I have to do some operation for process that i remove from listeA.
                  • 6. Re: comparison of 2 arraylist
                    800282
                    but same processes can be in both list. And my
                    processes run in thread so it can be a problem to
                    replace the whole list i think. I have to do some
                    operation for process that i remove from listeA.
                    Try this:
                    List A = Arrays.asList(new String[]{"a","b","c"        }); 
                    List B = Arrays.asList(new String[]{        "c","d","e"}); 
                            
                    Set S = new HashSet(A);                 // S = {a, b, c      }
                    S.addAll(B);                            // S = {a, b, c, d, e}
                    S.retainAll(B);                         // S = {      c, d, e}
                    A = Arrays.asList(S.toArray());         // A = S
                             
                    System.out.println("A = "+A);
                    If it doesn't work, you probably haven't overridden the equals(...) and hashCode methods.
                    Read this tutorial then:
                    http://java.sun.com/docs/books/tutorial/java/IandI/objectclass.html
                    and/or chapter 3 from Effective Java Programming Language Guide by Joshua Bloch:
                    http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf
                    for details on that.

                    Good luck.
                    • 7. Re: comparison of 2 arraylist
                      807607
                      Thanks for your answer, when you do S.addAll(B) and that both lists contains the c letter, is the c from S will be replace by the one from B ?
                      I used Collection.sort(listeA) for sorting my list and i redefined hashCode(), equals() and toString() method from object contained in my list, but it is based on the name of the object (process) and other attribute. Is it important on what base the method are redefined for the method you showed to me ?
                      Thanks.
                      • 8. Re: comparison of 2 arraylist
                        800282
                        Thanks for your answer, when you do S.addAll(B) and
                        that both lists contains the c letter, is the c from
                        S will be replace by the one from B ?
                        A set cannot contain duplicates. Since the hashCode's of "c" from list A and list B are the same, the second "c" will not be added.

                        and i redefined hashCode(), equals() and toString()
                        method from object contained in my list, but it is
                        based on the name of the object (process) and other
                        attribute. Is it important on what base the method
                        are redefined for the method you showed to me ?
                        It is only important to you, or your situation. You are free to implement it the way you like.

                        Thanks.
                        You're welcome.