如何在java中比较两个列表
我有两个列表,列表一,列表二,我想比较两者,如果两者相同,我想将列表项添加到其他列表ol1中,或者添加到ol2中 这里ElementRangeIndex是一个bean类,它包含一些字符串值 在比较这两个列表时,需要比较bean中的每个字符串值 我使用了下面的代码,但包含添加重复值,因为两个列表都有不同的对象如何在java中比较两个列表,java,list,Java,List,我有两个列表,列表一,列表二,我想比较两者,如果两者相同,我想将列表项添加到其他列表ol1中,或者添加到ol2中 这里ElementRangeIndex是一个bean类,它包含一些字符串值 在比较这两个列表时,需要比较bean中的每个字符串值 我使用了下面的代码,但包含添加重复值,因为两个列表都有不同的对象 public static Map<Integer, List<ElementRangeIndex>> compareLists(List<ElementRan
public static Map<Integer, List<ElementRangeIndex>> compareLists(List<ElementRangeIndex> listOne, List<ElementRangeIndex> listTwo) {
boolean indicator = false;
List<ElementRangeIndex> listOnes = new ArrayList<ElementRangeIndex>();
List<ElementRangeIndex> listTwos = new ArrayList<ElementRangeIndex>();
List<ElementRangeIndex> listThree = new ArrayList<ElementRangeIndex>();
Map<Integer, List<ElementRangeIndex>> map = new HashMap<Integer, List<ElementRangeIndex>>();
if (listOne!= null && listTwo!=null && listOne.size() == listTwo.size()) {
for (ElementRangeIndex listTwoData : listTwo) {
for (ElementRangeIndex listOneData : listOne) {
/* if (listOneData.getNamespaceUri().equals(listTwoData.getNamespaceUri())
&& listOneData.getCollation().equals(listTwoData.getCollation())
&& listOneData.getScalarType().equals(listTwoData.getScalarType())
&& listOneData.getLocalname().equals(listTwoData.getLocalname())) {*/
if ((listOneData.getNamespaceUri().hashCode()== listTwoData.getNamespaceUri().hashCode())
&& (listOneData.getCollation().hashCode() == listTwoData.getCollation().hashCode())
&& (listOneData.getScalarType().hashCode() == listTwoData.getScalarType().hashCode())
&& (listOneData.getLocalname().hashCode() == listTwoData.getLocalname().hashCode())) {
listOnes.add(listOneData);
if(listTwos.contains(listOneData))
listTwos.remove(listOneData);
if(listTwos.contains(listTwoData))
listTwos.remove(listTwoData);
if(listThree.contains(listOneData))
listThree.remove(listOneData);
if(listThree.contains(listTwoData))
listThree.remove(listTwoData);
}else{
if(!listOnes.contains(listOneData))
if(!listTwos.contains(listOneData))
listTwos.add(listOneData);
if(!listOnes.contains(listTwoData))
if(!listThree.contains(listTwoData))
listThree.add(listTwoData);
}
}
}
map.put(1,listOnes);
map.put(2, listTwos);
map.put(3, listThree);
}
return map;
}
公共静态映射比较列表(列表一、列表二){
布尔指示符=假;
List listOnes=new ArrayList();
List listTwos=新的ArrayList();
List listThree=新的ArrayList();
Map Map=newhashmap();
如果(listOne!=null&&listwo!=null&&listOne.size()==listwo.size()){
for(ElementRangeIndex listTwoData:listTwo){
对于(ElementRangeIndex listOneData:listOne){
/*如果(listOneData.getNamespaceUri().equals)(listTwoData.getNamespaceUri())
&&listOneData.getCollation().equals(listTwoData.getCollation())
&&listOneData.getScalarType()等于(listTwoData.getScalarType())
&&listOneData.getLocalname().equals(listTwoData.getLocalname())){*/
if((listOneData.getNamespaceUri().hashCode()==listTwoData.getNamespaceUri().hashCode())
&&(listOneData.getCollation().hashCode()==listTwoData.getCollation().hashCode())
&&(listOneData.getScalarType().hashCode()==listTwoData.getScalarType().hashCode())
&&(listOneData.getLocalname().hashCode()==listTwoData.getLocalname().hashCode()){
添加(listOneData);
if(listTwos.contains(listOneData))
删除(listOneData);
if(listTwos.contains(listTwoData))
删除(listTwoData);
if(listThree.contains(listOneData))
listThree.remove(listOneData);
if(listThree.contains(listTwoData))
listThree.remove(listTwoData);
}否则{
如果(!listOnes.contains(listOneData))
如果(!listTwos.contains(listOneData))
添加(listOneData);
如果(!listOnes.contains(listTwoData))
如果(!listThree.contains(listTwoData))
添加(listTwoData);
}
}
}
map.put(1,列表项);
map.put(2,listTwos);
地图放置(3,列表3);
}
返回图;
}
我的目标是将类似的列表项添加到一个列表(listOnes)中,左键仅添加到其他列表(listTwos),右键添加到其他列表(listThree)
谢谢,
Arjun如果您需要自己进行拆分,我可能会这样做:
leftOnly
。这将只包含列表1中存在的元素rightOnly
,则命名。这将只包含列表2中存在的元素intersectList
,它将包含两个列表中的元素leftOnly
可能包含太多的元素,因此我们需要对这些元素进行筛选。为此,我们使用迭代器对每个元素进行迭代,并检查它是否也包含在rightOnly
中。如果是,我们将该元素从leftOnly
和rightOnly
中删除,并将其添加到列表中代码>
为了加快这个过程(
列表中包含
和删除
都是线性操作),您可以将leftOnly
和righony
设置为LinkedHashSet
类型,它允许更快的操作,但不允许重复(在结果中使用重复项会破坏整个逻辑).如果您需要自己进行拆分,我可能会这样做:
leftOnly
。这将只包含列表1中存在的元素rightOnly
,则命名。这将只包含列表2中存在的元素intersectList
,它将包含两个列表中的元素leftOnly
可能包含太多的元素,因此我们需要对这些元素进行筛选。为此,我们使用迭代器对每个元素进行迭代,并检查它是否也包含在rightOnly
中。如果是,我们将该元素从leftOnly
和rightOnly
中删除,并将其添加到列表中代码>
为了加快这个过程(
列表中包含
和删除
都是线性操作),您可以将leftOnly
和righony
设置为LinkedHashSet
类型,它允许更快的操作,但不允许重复(在结果中使用重复项会破坏整个逻辑).使用Apache Commons Collections的类CollectionUtils
,该类提供了提取两个集合中包含的元素或仅一个集合中包含的元素的方法。使用LinkedHashSet
或类似的方法在拒绝重复项的同时保留插入顺序(如果需要)。使用Apache Commons Collections的类CollectionUtils
,该类提供了提取两个集合中包含的元素或仅一个集合中包含的元素的方法。使用LinkedHashSet
或类似的方法在拒绝重复项的同时保留插入顺序(如果需要)。