[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to compare two arraylists

Posted on 2005-05-17
31
Medium Priority
?
931 Views
Last Modified: 2008-03-06
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");
 
0
Comment
Question by:chaitu chaitu
  • 12
  • 11
  • 7
30 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 14017767
boolean equal = oldList.equals(newList);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 14017792
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 14017795
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:zzynx
ID: 14017796
quoet = quote
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 14017808
Then you need to do a set intersection. See Set.removeAll
0
 
LVL 37

Expert Comment

by:zzynx
ID: 14017809
>>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
 
LVL 37

Expert Comment

by:zzynx
ID: 14017811
Do you mean you want "666" as the result?
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 14017814
equals() will not help in my case;

i am ading Housedocumentcharges Object in list
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14017818
0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 14017828
If you do

      newList.removeAll(oldList);

newList only contains "666".
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14017850
           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
 
LVL 86

Expert Comment

by:CEHJ
ID: 14017874
:-)

Actually that should have said 'set subtraction' ;-)
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 14017885
why u always give example with HashSet();

we use Arraylist
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14017911
Then you can do

newList.retainAll(oldList);
0
 
LVL 37

Expert Comment

by:zzynx
ID: 14017923
>> 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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 14017928
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
 
LVL 37

Expert Comment

by:zzynx
ID: 14017948
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 14017955
>> O/P IS COMING AS 162 BUT IT SHOULD 888
Again, then why don't you use what I posted????

   newList.removeAll(oldList);
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 14017965
removeAll is working fine
0
 
LVL 37

Expert Comment

by:zzynx
ID: 14017980
So...
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 14017986
retainAll is not working
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14017992
(Which is what i posted originally). The Set thing was a bit of a red herring along the way sorry
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14017997
>> retainAll is not working

Use what i posted originally
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14018007
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14018020
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
 
LVL 37

Expert Comment

by:zzynx
ID: 14018042
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 14018051
yes i made a mistake ;i have to split the points;what do u say CEHJ
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14018093
Yes that's OK.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 14024784
Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14025326
:-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question