wood burning stoves
The moose likes Beginning Java and the fly likes ArrayList Compare Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList Compare" Watch "ArrayList Compare" New topic

ArrayList Compare

Ananth Ram
Ranch Hand

Joined: Jan 18, 2001
Posts: 99
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.

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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

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

You down with OOP? Yeah you know me!
Ananth Ram
Ranch Hand

Joined: Jan 18, 2001
Posts: 99
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.


Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link: http://aspose.com/file-tools
subject: ArrayList Compare
It's not a secret anymore!