在Java中查找两个ArrayLists之间的不同元素

我如何知道java中2个数组列表之间的不同元素? 我需要确切的元素而不是布尔值,可以使用removeAll()来检索。

使用Apache Commons Collections ( javadoc ):

 CollectionUtils.disjunction(a, b); 

另请参阅: Effective Java,第2版第47项:了解并使用库 (作者仅提到了JDK的内置库,但我认为其他库的推理也是如此。)

如果我正确理解了您的问题,那么下面的代码中的方法nonOverLap可以让您:

  Collection union(Collection coll1, Collection coll2) { Set union = new HashSet<>(coll1); union.addAll(new HashSet<>(coll2)); return union; }  Collection intersect(Collection coll1, Collection coll2) { Set intersection = new HashSet<>(coll1); intersection.retainAll(new HashSet<>(coll2)); return intersection; }  Collection nonOverLap(Collection coll1, Collection coll2) { Collection result = union(coll1, coll2); result.removeAll(intersect(coll1, coll2)); return result; } 

这取决于你想要检查什么。

  1. 如果你想获得两个列表的所有唯一元素(即第一个列表唯一的所有元素的总和以及第二个列表唯一的所有元素的总和),也称为对称差异,你可以使用如上所述的apjunction方法来自Apache Commons Collections 4.0 :

     CollectionUtils.disjunction(a, b); 
  2. 如果你想只从一个列表中获取所有唯一元素(即只存在于一个列表中但在另一个列表中不存在的元素),也称为相对补码 ,则可以使用Apache中的 减法方法从该列表中减去另一个列表Commons Collections 4.0 :

     CollectionUtils.subtract(a, b); //gives all unique elements of a that don't exist in b 
 import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; public class CompareTwoList { public CompareTwoList() { // TODO Auto-generated constructor stub } public static void main(String[] args) { List ls1 = new ArrayList(); ls1.add("a"); ls1.add("b"); ls1.add("c"); ls1.add("d"); List ls2 = new ArrayList(); ls2.add("a"); ls2.add("b"); ls2.add("c"); ls2.add("d"); ls2.add("e"); Set set1 = new HashSet(); set1.addAll(ls1); Set set2 = new HashSet(); set2.addAll(ls2); set2.removeAll(set1); //set.addAll(ls1); //set.addAll(ls1); for (String diffElement : set2) { System.out.println(diffElement.toString()); } } } 
 LinkedHashMap table; for each element e of array A if table.get(e) != null table.put( e, table.get(e) + 1 ) else table.put( e, 0 ) //Do the same for array B for each element e of array B if table.get(e) != null table.put( e, table.get(e) + 1 ) else table.put( e, 0 ) 

在for循环结束时,表中值为0的元素是不同的元素。

调用ReturnArrayListDiffElements方法传递两个数组列表。 将返回一个数组列表,它是两个传递的数组列表之间的差异

 public ArrayList ReturnArrayListDiffElements(ArrayList arrList1, ArrayList arrList2){ ArrayList List1 = new ArrayList(); ArrayList List2 = new ArrayList(); ArrayList List3 = new ArrayList(); ArrayList List4 = new ArrayList(); List1.addAll(arrList1); List2.addAll(arrList2); List3 = ReturnArrayListCommonElements(List1,List2); List1.removeAll(List3); List2.removeAll(List3); if(List1.size() > 0){ List4.add("Distinct elements in Array List 1"); List4.addAll(List1); } if(List2.size() > 0){ List4.add("Distinct elements in Array List 2"); List4.addAll(List2); } return List4; } public ArrayList ReturnArrayListCommonElements(ArrayList arrList1, ArrayList arrList2){ ArrayList List1 = new ArrayList(); ArrayList List2 = new ArrayList(); ArrayList List1A = new ArrayList(); ArrayList List2A = new ArrayList(); ArrayList List1B = new ArrayList(); ArrayList List3 = new ArrayList(); List1.addAll(arrList1); List2.addAll(arrList2); List1A.addAll(arrList1); List2A.addAll(arrList2); List1B.addAll(arrList1); int intList1Size, intList2Size; List1.removeAll(List2); intList1Size = List1.size(); List2.removeAll(List1A); intList2Size = List2.size(); if (intList1Size == 0 && intList2Size ==0) { List3.addAll(List1B); return List3; } else { List3.addAll(List1B); List1B.removeAll(List2A); List3.removeAll(List1B); return List3; } }