• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Compare two lists

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
This tiny ad is wafer thin:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic