how to compare two arraylists

i have two arraylists;how to compare them;

 ArrayList newList = new ArrayList();
  newList.add("162");
  newList.add("176");
  newList.add("666");
  ArrayList oldList = new ArrayList();
  oldList.add("162");
  oldList.add("176");
 
LVL 20
chaitu chaituAsked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
If you do

      newList.removeAll(oldList);

newList only contains "666".
0
 
CEHJCommented:
boolean equal = oldList.equals(newList);
0
 
zzynxSoftware engineerCommented:
Indeed. I quoet from the docs:

List.equals()

public boolean equals(Object o)

Compares the specified object with this list for equality.
Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
chaitu chaituAuthor Commented:
in the oldlist i have      chargeid's of Housedocumentcharges Object
in the newlist also  i have      chargeid's of Housedocumentcharges Object;

i want to find out what is modified chargeid or newchargeid added in newlist
0
 
zzynxSoftware engineerCommented:
quoet = quote
0
 
CEHJConnect With a Mentor Commented:
Then you need to do a set intersection. See Set.removeAll
0
 
zzynxSoftware engineerCommented:
>>in the oldlist i have     chargeid's of Housedocumentcharges Object
>>in the newlist also  i have     chargeid's of Housedocumentcharges Object;
>>i want to find out what is modified chargeid or newchargeid added in newlist
I don't understand. Can your rephrase or give an example?
0
 
zzynxSoftware engineerCommented:
Do you mean you want "666" as the result?
0
 
chaitu chaituAuthor Commented:
equals() will not help in my case;

i am ading Housedocumentcharges Object in list
0
 
CEHJCommented:
0
 
CEHJCommented:
           Set _new = new HashSet();
        _new.add("162");
        _new.add("176");
        _new.add("666");
        Set _old = new HashSet();
        _old.add("162");
        _old.add("176");
            _new.removeAll(_old);
            System.out.println(_new);
0
 
CEHJCommented:
:-)

Actually that should have said 'set subtraction' ;-)
0
 
chaitu chaituAuthor Commented:
why u always give example with HashSet();

we use Arraylist
0
 
CEHJCommented:
Then you can do

newList.retainAll(oldList);
0
 
zzynxSoftware engineerCommented:
>> why u always give example with HashSet();
>> we use Arraylist
Then why is the code I posted not good enough to deserve part of the points?
0
 
chaitu chaituAuthor Commented:
ArrayList newList = new ArrayList();
  newList.add("162");
  newList.add("888");

  ArrayList oldList = new ArrayList();
  oldList.add("162");
  oldList.add("176");
 
   newList.retainAll(oldList);
          System.out.println(" charge modified"+newList);

O/P IS COMING AS 162 BUT IT SHOULD 888
0
 
zzynxSoftware engineerCommented:
>> Then you can do
>> newList.retainAll(oldList);

Which gives you

   newList = { "162", "176" };

Is that what is wanted???
>> i want to find out what is modified chargeid or newchargeid added in newlist
0
 
zzynxSoftware engineerCommented:
>> O/P IS COMING AS 162 BUT IT SHOULD 888
Again, then why don't you use what I posted????

   newList.removeAll(oldList);
0
 
chaitu chaituAuthor Commented:
removeAll is working fine
0
 
zzynxSoftware engineerCommented:
So...
0
 
chaitu chaituAuthor Commented:
retainAll is not working
0
 
CEHJCommented:
(Which is what i posted originally). The Set thing was a bit of a red herring along the way sorry
0
 
CEHJCommented:
>> retainAll is not working

Use what i posted originally
0
 
CEHJCommented:
0
 
CEHJCommented:
To make it perfectly clear:


            ArrayList newList = new ArrayList();
            newList.add("162");
            newList.add("888");

            ArrayList oldList = new ArrayList();
            oldList.add("162");
            oldList.add("176");

            newList.removeAll(oldList);
            System.out.println(newList);


(Prints '[888]')
0
 
zzynxSoftware engineerCommented:
CEHJ,
>> (Which is what i posted originally)
I agree that you mentioned removeAll(), but the context
>> you need to do a set intersection
was clearly not the right track.

>> To make it perfectly clear:
Now, isn't that what I already told?

chaituu, I really feel like a point split is the right thing to do.





0
 
chaitu chaituAuthor Commented:
yes i made a mistake ;i have to split the points;what do u say CEHJ
0
 
CEHJCommented:
Yes that's OK.
0
 
zzynxSoftware engineerCommented:
Thanks
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.