aspose file tools*
The moose likes Java in General and the fly likes Compare two lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Compare two lists" Watch "Compare two lists" New topic
Author

Compare two lists

Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
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

Joined: Aug 05, 2005
Posts: 208
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 ]

Entia non sunt multiplicanda praeter necessitatem
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
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

Joined: Aug 05, 2005
Posts: 3646
    
  15


Joanne
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
Thank you people. Thanks for the guidance.

Mr. Hassel, your solution perfectly fitted into my requirement.
Thanks & have a wonderful day ahead.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compare two lists