• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help needed in comparing 2 arraylists

 
Keerthi Kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



>
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50278
80
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell Ritchie/Seetaram,

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

>
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noo. Hint: why do you need nested for loops and you are comparison logic is bad
 
Nikhil Padharia
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic