• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compare two lists

 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two lists contaning objects of similar type. How do we compare two lists , in other words, how do we see see if a particular object present in one list is present in the other list also?

Please help.

Thanks
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The List interface defines the contains method:

contains

boolean contains(Object o)

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).


http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html
[ December 13, 2006: Message edited by: �dne Brunborg ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vani Shastri:
how do we see see if a particular object present in one list is present in the other list also?


Do you want the actually objects in the heap to be equal , then try for == operator , this will compare the actuall objects in the heap.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummmm.... yeah.

The standard List implementations all define equals() to compare the contents of the two lists for equality using the equals() methods of the contained objects. Just use that.
 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two lists -

List list1 = (List) model
.getlist1(); eg [obj1, obj2]
List list2 = (List) model
.getlist2(); eg [obj1, obj2, obj3, obj4]

both the lists contain objects of similar type.

Basically list1 is a subset of list2. I want to retreive all the objects present in
list2 which is present in list1 also and remove them from list2.


how do i use boolean contains(object o) in this case ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vani wrote:
I want to retreive all the objects present in
list2 which is present in list1 also and remove them from list2.
how do i use boolean contains(object o) in this case ?



Not at all.
Better use bulk operations:

List retrieve= new ArrayList(list2);
retrieve.retainAll(list1);
list2.removeAll(list1);


Yours,
Bu.
 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you people. Thanks for the guidance.

Mr. Hassel, your solution perfectly fitted into my requirement.
Thanks & have a wonderful day ahead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic