I have two class lists like List<Aclass>
A1 and List<Bclass>
b1 and both lists contain one field which is common. So by using this field I have to compare those two list? Please can anyone help me? Be ensure that those class lists are different.
Asked
Active
Viewed 1.5k times
0

Arjan Tijms
- 37,782
- 12
- 108
- 140

vinod
- 1,178
- 4
- 16
- 42
-
What do you mean one field is different? Is it a list of some object such as `Person` where an attribute such as `name` is different? Please try to be more clear when you ask questions. – adarshr Mar 12 '11 at 13:51
-
1And what do you mean 'compare'? You want to sort the lists or something? – adarshr Mar 12 '11 at 13:52
-
2And, please, don't use signature in posts, it's already here, below the post. Thanks. – YasirA Mar 12 '11 at 13:54
-
@adarshr field is different means both list are different types – vinod Mar 12 '11 at 14:54
2 Answers
5
I'm not sure I fully understand the question, but you're asking for something like this?
public boolean compare (List<AClass> listA, List<BClass> listB) {
if (listA.size() != listB.size ()) {
return false;
}
for (int i=0; i<listA.size(); i++) {
AClass aClass = (AClass) listA.get(i);
BClass bClass = (BClass) listB.get(i);
// This is example for numeric comparison
// For String, use !equals()
if (aClass.commonField != bClass.commonField) return false;
}
return true;
}
Both lists should be sorted by that commonField field
-
Thanks Aleadam i think i am very near to solution!!! you got my problem correctly – vinod Mar 12 '11 at 15:00
-
@vinod: If you think this is the solution, mark it as such by clicking the checkmark beside the post. – Paŭlo Ebermann Mar 13 '11 at 02:12
-
I edited a couple of typos. Remember that such a short and simple method will only work if the lists are sorted. If the class fields are going to be public then you should make sure they're not null. A better option would be to create a public method `compareField()` in AClass and BClass – Aleadam Mar 13 '11 at 04:46
0
Checkout the javadoc for List.equals():
two lists are defined to be equal if they contain the same elements in the same order.
The equals method can be used here and of course ignores the generic types you have declared the two lists to be. The equals method for AbstractList
(which ArrayList and a bunch of other list classes implements) will iterate over both lists and call equals on each of the elements in order, once done it makes sure there aren't any items left in either list.
Given all this, you will be able to call the equals method to determine weather two lists contain the same elements:
List<Aclass> A1 = new ArrayList<Aclass>();
List<Bclass> b1 = new ArrayList<Bclass>();
// getElement returns a private field that can be cast to both Aclass and Bclass
A1.add(getElement());
b1.add(getElement());
if (A1.equals(b1)) {
System.out.println("two lists are equal");
}

krock
- 28,904
- 13
- 79
- 85
-
@Yasir yea I don't know what's going on at the moment, my keyboard is not playing fair. – krock Mar 12 '11 at 14:11
-