aspose file tools*
The moose likes Java in General and the fly likes Comparing two ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparing two ArrayList" Watch "Comparing two ArrayList" New topic
Author

Comparing two ArrayList

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9384
    
    2

Guys,

I want to compare two ArrayLists and extract the values that do not match. How could I do this? The ArrayList simply containes numbers.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Are you supposed to do it the proper way with the methods of the List interfaceor are you supposed to do it the hard way by iterating through both Lists?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Follow the method links from List to Collection, then to Set, that might give better hints what they are supposed to do.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9384
    
    2

I have two Lists, one is big (with a max of 10 elements) and one is small which can be either equal or less than the other List. I want to compare both these Lists and get the ones that do not match.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Jothi,

Add your first list to a Set. Then add each object from second list to the same set. If it already exists "add()" method would return false . If its false add it to a temporary array !!


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Perhaps you can play around with the bulk operators: addAll, removeAll and retainAll. You may need to use some extra collections for that to work, because these methods modify the original.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Now Rob has hinted about three methods . . .

Close examination of the corresponding methods in the java.util.Set interface will give some very useful information.
Prasad Kumbhare
Greenhorn

Joined: Oct 18, 2007
Posts: 26

I would do it using , contains method. Some steps would be

1) using iterator traverse through second list which has less elements
2) get next element from second list
3) use contains method to check if that element exists in first list which is big one. If no then add it into temp list
4) Once loop got executed your temp list will have elements from second list which are not there from first list.

I hope this helps.

Thanks

Thanks,
Prasad Kumbhare
SCJP 5.0, SCWCD 5.0, SCBCD 5.0
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Hi please use Collection Utils of Apache to do this Job .


Hope this helps !!!


Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rob Prime wrote:Perhaps you can play around with the bulk operators: addAll, removeAll and retainAll. You may need to use some extra collections for that to work, because these methods modify the original.


Really Good Hint . and i am wondering how you are point out exactly Everytime
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Experience mostly, plus a pretty good knowledge of parts of the API.
 
Consider Paul's rocket mass heater.
 
subject: Comparing two ArrayList