发现在Java中2的ArrayList之间的不同元素 [英] Finding the different elements between two ArrayLists in Java

查看:126
本文介绍了发现在Java中2的ArrayList之间的不同元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能知道在Java 2数组列表之间的不同的元素?我需要的确切元素没有可使用检索一个布尔值的removeAll()

How can I know the different element between 2 array list in java? I need the exact element not a Boolean value which can be retrieved using removeAll().

推荐答案

如果我理解正确你的问题,然后跟随在code方法非重叠下面应该让你即:

If I understood your question correctly then following method nonOverLap in the code below should get you that:

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;
}

有关详细信息和上述解释:<一href=\"http://$c$c.hammerpig.com/find-the-difference-between-two-lists-in-java.html\">http://$c$c.hammerpig.com/find-the-difference-between-two-lists-in-java.html

For more details and explanation of above: http://code.hammerpig.com/find-the-difference-between-two-lists-in-java.html

这篇关于发现在Java中2的ArrayList之间的不同元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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