我如何计算两个ArrayLists之间的差异?

我有两个ArrayLists。

ArrayList A包含

['2009-05-18','2009-05-19','2009-05-21'] 

ArrayList B包含['2009-05-18','2009-05-18','2009-05-19','2009-05-19','2009-05-20','2009-05-21','2009-05-21','2009-05-22']

我必须比较ArrayLst A和ArrayLst B。 结果ArrayList应该包含ArrayList中不存在的List。ArrayList的结果应该是

[ '2009-05-20', '2009-05-22']

如何比较?

在Java中,您可以使用Collection接口的removeAll方法。

 // Create a couple ArrayList objects and populate them // with some delicious fruits. Collection firstList = new ArrayList() {{ add("apple"); add("orange"); }}; Collection secondList = new ArrayList() {{ add("apple"); add("orange"); add("banana"); add("strawberry"); }}; // Show the "before" lists System.out.println("First List: " + firstList); System.out.println("Second List: " + secondList); // Remove all elements in firstList from secondList secondList.removeAll(firstList); // Show the "after" list System.out.println("Result: " + secondList); 

上面的代码将产生以下输出:

 First List: [apple, orange] Second List: [apple, orange, banana, strawberry] Result: [banana, strawberry] 

你已经有了正确的答案。 如果你想在列表(集合)之间进行更复杂和有趣的操作,可以使用apache commons集合 ( CollectionUtils )。它允许你进行连接/分离,find交集,检查一个集合是否是另一个集合的一个子集。

编辑:原来的问题没有指定语言。 我的答案是在C#中。

你应该使用HashSet来达到这个目的。 如果您必须使用ArrayList,则可以使用以下扩展方法:

 var a = arrayListA.Cast<DateTime>(); var b = arrayListB.Cast<DateTime>(); var c = b.Except(a); var arrayListC = new ArrayList(c.ToArray()); 

使用HashSet …

 var a = new HashSet<DateTime>(); // ...and fill it var b = new HashSet<DateTime>(); // ...and fill it b.ExceptWith(a); // removes from b items that are in a 

我用过Guava Sets.difference 。

参数是集合而不是通用集合,但是从任何集合(具有唯一项目)创build集合的方便方式是Guava ImmutableSet.copyOf (Iterable)。

(我首先在相关/复制问题上发布了这个问题 ,但是我也在这里复制它,因为我觉得这是一个很好的select,至今还没有。

虽然在Java 8中这是一个非常古老的问题,但是你可以做类似的事情

  List<String> a1 = Arrays.asList("2009-05-18", "2009-05-19", "2009-05-21"); List<String> a2 = Arrays.asList("2009-05-18", "2009-05-18", "2009-05-19", "2009-05-19", "2009-05-20", "2009-05-21","2009-05-21", "2009-05-22"); List<String> result = a2.stream().filter(elem -> !a1.contains(elem)).collect(Collectors.toList()); 

`

我想你正在谈论C#。 如果是这样,你可以试试这个

  ArrayList CompareArrayList(ArrayList a, ArrayList b) { ArrayList output = new ArrayList(); for (int i = 0; i < a.Count; i++) { string str = (string)a[i]; if (!b.Contains(str)) { if(!output.Contains(str)) // check for dupes output.Add(str); } } return output; } 

您好使用这个类,这将比较这两个列表,并显示完全不符合双方列表。

 import java.util.ArrayList; import java.util.List; public class ListCompare { /** * @param args */ public static void main(String[] args) { List<String> dbVinList; dbVinList = new ArrayList<String>(); List<String> ediVinList; ediVinList = new ArrayList<String>(); dbVinList.add("A"); dbVinList.add("B"); dbVinList.add("C"); dbVinList.add("D"); ediVinList.add("A"); ediVinList.add("C"); ediVinList.add("E"); ediVinList.add("F"); /*ediVinList.add("G"); ediVinList.add("H"); ediVinList.add("I"); ediVinList.add("J");*/ List<String> dbVinListClone = dbVinList; List<String> ediVinListClone = ediVinList; boolean flag; String mismatchVins = null; if(dbVinListClone.containsAll(ediVinListClone)){ flag = dbVinListClone.removeAll(ediVinListClone); if(flag){ mismatchVins = getMismatchVins(dbVinListClone); } }else{ flag = ediVinListClone.removeAll(dbVinListClone); if(flag){ mismatchVins = getMismatchVins(ediVinListClone); } } if(mismatchVins != null){ System.out.println("mismatch vins : "+mismatchVins); } } private static String getMismatchVins(List<String> mismatchList){ StringBuilder mismatchVins = new StringBuilder(); int i = 0; for(String mismatch : mismatchList){ i++; if(i < mismatchList.size() && i!=5){ mismatchVins.append(mismatch).append(","); }else{ mismatchVins.append(mismatch); } if(i==5){ break; } } String mismatch1; if(mismatchVins.length() > 100){ mismatch1 = mismatchVins.substring(0, 99); }else{ mismatch1 = mismatchVins.toString(); } return mismatch1; } } 

这个工作也与Arraylist

  // Create a couple ArrayList objects and populate them // with some delicious fruits. ArrayList<String> firstList = new ArrayList<String>() {/** * */ private static final long serialVersionUID = 1L; { add("apple"); add("orange"); add("pea"); }}; ArrayList<String> secondList = new ArrayList<String>() { /** * */ private static final long serialVersionUID = 1L; { add("apple"); add("orange"); add("banana"); add("strawberry"); }}; // Show the "before" lists System.out.println("First List: " + firstList); System.out.println("Second List: " + secondList); // Remove all elements in firstList from secondList secondList.removeAll(firstList); // Show the "after" list System.out.println("Result: " + secondList); 

你只是比较string。

将ArrayList A中的值作为HashTable A中的键
将ArrayList B中的值作为HashTable B中的键

然后,对于HashTable A中的每个密钥,如果存在,则将其从HashTable B中删除。

在HashTable B中剩下的是在ArrayList A中不是值的string(键)

C#(3.0)为响应代码请求而添加的示例:

 List<string> listA = new List<string>{"2009-05-18","2009-05-19","2009-05-21'"}; List<string> listB = new List<string>{"2009-05-18","2009-05-18","2009-05-19","2009-05-19","2009-05-20","2009-05-21","2009-05-21","2009-05-22"}; HashSet<string> hashA = new HashSet<string>(); HashSet<string> hashB = new HashSet<string>(); foreach (string dateStrA in listA) hashA.Add(dateStrA); foreach (string dateStrB in listB) hashB.Add(dateStrB); foreach (string dateStrA in hashA) { if (hashB.Contains(dateStrA)) hashB.Remove(dateStrA); } List<string> result = hashB.ToList<string>(); 

在使用stream的Java 8中,实际上它非常简单。

 List<String> listA = Arrays.asList("2009-05-18","2009-05-19","2009-05-21"); List<String> listB = Arrays.asList("2009-05-18","2009-05-18","2009-05-19","2009-05-19", "2009-05-20","2009-05-21","2009-05-21","2009-05-22"); List<String> result = listB.stream() .filter(not(new HashSet<>(listA)::contains)) .collect(Collectors.toList()); 

这需要一个自定义的实用工具方法,而not部分虽然:

 private static <T> Predicate<T> not(Predicate<T> predicate) { return predicate.negate(); } 

你也可以单独定义集合。 如果有一个filterOut方法,它会更好看。