如何计算两个 ArrayList 之间的差异? [英] How can I calculate the difference between two ArrayLists?
本文介绍了如何计算两个 ArrayList 之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个 ArrayList.
I have two 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']
我必须比较 ArrayList A 和 ArrayList B.结果 ArrayList应该包含在 ArrayList A 中不存在的 List.
I have to compare ArrayList A and ArrayList B. The result ArrayList should contain the List which does not exist in ArrayList A.
ArrayList 结果应该是:
['2009-05-20','2009-05-22']
如何比较?
推荐答案
在 Java 中,您可以使用 Collection
接口的removeAll
方法.
In Java, you can use the Collection
interface's removeAll
method.
// 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);
以上代码将产生以下输出:
The above code will produce the following output:
First List: [apple, orange]
Second List: [apple, orange, banana, strawberry]
Result: [banana, strawberry]
这篇关于如何计算两个 ArrayList 之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文