Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparing two ArrayList

 
Joe Harry
Ranch Hand
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi please use Collection Utils of Apache to do this Job .


Hope this helps !!!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Experience mostly, plus a pretty good knowledge of parts of the API.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic