如何在Java中比较两个不同的列表对象? [英] How to compare two different list object in java?
本文介绍了如何在Java中比较两个不同的列表对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个类列表,例如 List< Aclass>
A1和 List< Bclass>
b1,并且两个列表都包含一个共同的字段.因此,通过使用此字段,我必须比较这两个列表?有人可以帮我吗?确保这些类别列表不同.
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.
推荐答案
我不确定我是否完全理解这个问题,但是您要的是这样的东西?
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);
if (aClass.commonField != bClass.commonField) return false;
}
return true;
}
两个列表都应按该commonField字段排序
Both lists should be sorted by that commonField field
这篇关于如何在Java中比较两个不同的列表对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文