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
posted
0
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)).
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.
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: 3011
9
posted
0
Joanne
Burkhard Hassel
Ranch Hand
Joined: Aug 25, 2006
Posts: 1274
posted
0
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
posted
0
Thank you people. Thanks for the guidance.
Mr. Hassel, your solution perfectly fitted into my requirement. Thanks & have a wonderful day ahead.