File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Help needed in comparing 2 arraylists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help needed in comparing 2 arraylists" Watch "Help needed in comparing 2 arraylists" New topic
Author

Help needed in comparing 2 arraylists

Keerthi Kumar
Ranch Hand

Joined: Apr 20, 2009
Posts: 105
Hi all,

I have a scenario, in which, I need to compare with old and new values, which will be 2 different array lists. Also, i need to find the new element or item added in the 2nd array list and print that value. Below is my code snippet.



>


Cheers,
Keerthi Kumar N
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

just loop through new list and check any value is already there in a old list. if not add it to old list or print what ever you want.

does that solve your problem?
Keerthi Kumar
Ranch Hand

Joined: Apr 20, 2009
Posts: 105
Hi Seetaram,

Thanks a lot for your reply. I am trying to fetch the new value from the new list. could you please help me on how to search for the new value in the second list?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
You can use the methods like indexOf() to find an element in a List. That will only give the index of the first element found, using the equals() method to test for equality.
Keerthi Kumar
Ranch Hand

Joined: Apr 20, 2009
Posts: 105
Hi Campbell Ritchie/Seetaram,

Please see the below code. Please correct me if im wrong.

>
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Noo. Hint: why do you need nested for loops and you are comparison logic is bad
Nikhil Padharia
Ranch Hand

Joined: Apr 19, 2012
Posts: 37
Use contains method of the collection interface to check whether the value is present in old list, while iterating through new list.
Here, If you have User defined objects in collection then you need to override equals method of the object.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Nikhil Padharia wrote:Use contains method of the collection interface to check whether the value is present in old list, while iterating through new list.

in fact no difference since contains directly call the indexOf(Object)
S Subbu
Ranch Hand

Joined: Mar 20, 2012
Posts: 87

HI Kumar ,

1.Check the size of old list.

2.Iterate new list ,and get the element in that list and check if that element exist in old list or not , if not add these elements to old list .

3.At last Check the size of old list .


If all The element are present in one list use above Code ,

Other wise Use Another ArrayList for count newly added records size and newly Added elements




May be it will help to You.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help needed in comparing 2 arraylists