我怎么能计算两个的ArrayList的区别? [英] How can I calculate the difference between two ArrayLists?

查看:96
本文介绍了我怎么能计算两个的ArrayList的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个的ArrayList

I have two ArrayLists.

ArrayList中A包含

ArrayList A contains

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

ArrayList的B中<$c$c>['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中存在A.名单
ArrayList的结果应该是

I have to compare ArrayLst A and ArrayLst B . The result ArrayList should contain the List which does not exist in ArrayList A. ArrayList result should be

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

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

如何比较?

推荐答案

在Java中,你可以使用<$c$c>Collection接口的<一个href=\"http://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#removeAll-java.util.Collection-\"><$c$c>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);

以上code将产生以下的输出:

The above code will produce the following output:

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

这篇关于我怎么能计算两个的ArrayList的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆