• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList Compare

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

public class ContactVO implements Serializable{

private String email;
private String name;
}

I created 2 arraylist based on the above VO which holds name and email address.

I would like to compare the 2 arraylist based on email address so that the new arraylist should not hold duplicate email address. Could some one give me some tip on the approach.

Thanks,
Ananth
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what you could do is something like the following.




this should be kind of what you are looking for.
this will compare each item in the firstlist with the same
corresponding index as the second.

if you want to search the whole second list for the i'th
value in the firtlist, you'll need to implement a nested
search.

and by the way, the ArrayList.get(index) returns an object, thats
why I casted.

I strongly recommend that you use the API provided on suns website, it
helps out a whole lot.

click here for the arraylist on javas site

Justin Fox
 
Ananth Ram
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the guidelines, and the 2 arraylist are of different size.

What shall I do to compare the email address and store it in a new arraylist.

Thanks,

Ananth
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you be ok to just put them into a Set? Sets reject duplicate entries based on e1.equals(e2), so if you addAll() with both lists you'll wind up witl all unique values.
 
We find this kind of rampant individuality very disturbing. But not this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic