Results 1 to 6 of 6

Thread: Compare ArrayList of objects

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Compare ArrayList of objects

    Hello,
    I am trying to do a program in java which will compare two arraylist objects and will place a third difference that exist between the other two. It this possible, can I achieve this, If yes then how can I compare two objects of a arraylist. Any help on this will be highly appreciated. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    356

    Re: Compare ArrayList of objects

    Hello,
    I have a code with me, this compares the two arraylist objects , this is quite simple and you can do this easily.
    Here is the code for this
    Code:
    ArrayList ls1 = new ArrayList();
    ArrayList ls2 = new ArrayList();
     
    // ...
    
    ArrayList arr1 = new ArrayList();
    Collections.copy(arr1, ls1);
    ArrayList arr2 = new ArrayList();
    Collections.copy(arr2, ls2);
    
    arr1.removeAll(ls2);
    arr2.removeAll(ls1);
    
    ArrayList df = new ArrayList();
    df.addAll(arr1);
    df.addAll(arr2);

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Compare ArrayList of objects

    Hello,
    I am trying to do a program in java which will compare two arraylist objects and will place a third difference that exist between the other two. It this possible, can I achieve this, If yes then how can I compare two objects of a arraylist.
    Want differences (the set difference) between the 1st and 2nd, or between the 2nd and 1? If you are talking about the same then this is the code for you
    Code:
    List list = <TonType> new ArrayList <TonType>(lst1);
    list.removeAll(lst2);

  4. #4
    Join Date
    Nov 2009
    Posts
    347

    Re: Compare ArrayList of objects

    Hello,
    Of course it's possible to just go an ArrayList and see if your object is present or not in your 2nd ArrayList. Depending on the result or not you put the object in the ArrayList 3. And you delete the object if it is present in the ArrayList 2. And in the end you put what remains of the ArrayList 2 in 3. I think you understand what I am trying to say.

  5. #5
    Join Date
    Nov 2009
    Posts
    446

    Re: Compare ArrayList of objects

    Hello,
    In my opinion, he wants to put in a 3rd ArrayList:
    - The items in the list 1 but not in the list 2.
    - The elements present in list 2 but not in the list 1.
    I had to encounter the same problem one day. I filled a ComboBox in a modified form. The choice of the comboBox were fields of a column of one of my tables. A validation of the change I had to compare elements of the old and the new combobox. As the user could add or delete items in the comboBox, so I was facing the same problem:
    - I was watching the commonalities between the old and the new combo.
    - If values were present in the former and more in the news, I had to remove the record containing that value in the database.
    - If values were present in the new and not old, I had to add a record in the database with this new value.
    To simplify my task, I had done something not very clean but good in a hurry I improvised. I select all records in the database containing the values of the old combo, delete and add all the values of the combo, even recreate records that already existed.

  6. #6
    Join Date
    Nov 2009
    Posts
    356

    Re: Compare ArrayList of objects

    Hello,
    You have had your first ArrayList and your second b; You take a temp temporary ArrayList containing all records of a;
    Code:
    / / ON has removed all elements present in b
    a.removeAll(b);
    / / We enelve then all elements b in a => aa temp as amended;
    b.removeAll(Temp);
     
    / / Then it was that differences in a and b. Just take a and add all the records of b;
    a.addAll(b);
    / / And thou shalt have the differences!

Similar Threads

  1. Using an ArrayList
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 12:56 PM
  2. Multithreading and ArrayList
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 05:16 AM
  3. Initialize an ArrayList
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 04:13 AM
  4. ArrayList not working
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 07-02-2010, 04:18 AM
  5. Compare two DateTime objects
    By teena_pansare in forum Software Development
    Replies: 3
    Last Post: 28-11-2009, 05:38 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,413,114.15749 seconds with 16 queries