如何从两个数组列表中删除常用值

时间:2013-03-22 16:33:19

标签: java list collections arraylist

我们如何从两个ArrayList中删除常用值。 让我们考虑我有两个Arraylist,如下所示

ArrayList1= [1,2,3,4]
ArrayList1= [2,3,4,6,7]

我想将结果作为

ArrayListFinal= [1,6,7]

有人可以帮帮我吗?

4 个答案:

答案 0 :(得分:36)

以下是完成任务所需遵循的算法:

  • 构建两个数组的并集
  • 构建两个数组的交集
  • 从联合中减去交集以获得结果

Java集合支持addAllremoveAllretainAll。使用addAll构建联合,retainAll用于构建交叉点,removeAll用于减法,like this

// Make the two lists
List<Integer> list1 = Arrays.asList(1, 2, 3, 4);
List<Integer> list2 = Arrays.asList(2, 3, 4, 6, 7);
// Prepare a union
List<Integer> union = new ArrayList<Integer>(list1);
union.addAll(list2);
// Prepare an intersection
List<Integer> intersection = new ArrayList<Integer>(list1);
intersection.retainAll(list2);
// Subtract the intersection from the union
union.removeAll(intersection);
// Print the result
for (Integer n : union) {
    System.out.println(n);
}

答案 1 :(得分:17)

您实际上是在询问Symmetric Difference

List<Integer> aList = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
List<Integer> bList = new ArrayList<>(Arrays.asList(2, 3, 4, 6, 7));
// Union is all from both lists.
List<Integer> union = new ArrayList(aList);
union.addAll(bList);
// Intersection is only those in both.
List<Integer> intersection = new ArrayList(aList);
intersection.retainAll(bList);
// Symmetric difference is all except those in both.    
List<Integer> symmetricDifference = new ArrayList(union);
symmetricDifference.removeAll(intersection);

System.out.println("aList: " + aList);
System.out.println("bList: " + bList);
System.out.println("union: " + union);
System.out.println("intersection: " + intersection);
System.out.println("**symmetricDifference: " + symmetricDifference+"**");

打印:

aList: [1, 2, 3, 4]
bList: [2, 3, 4, 6, 7]
union: [1, 2, 3, 4, 2, 3, 4, 6, 7]
intersection: [2, 3, 4]
**symmetricDifference: [1, 6, 7]**

答案 2 :(得分:4)

您可以使用以下内容:

    ArrayList <Integer> first = new ArrayList <Integer> ();
    ArrayList <Integer> second = new ArrayList <Integer> ();
    ArrayList <Integer> finalResult = new ArrayList <Integer> ();

    first.add(1);
    first.add(2);
    first.add(3);
    first.add(4);

    second.add(2);
    second.add(3);
    second.add(4);
    second.add(6);
    second.add(7);

    for (int i = 0; i < first.size(); i++){

        if (!second.contains(first.get(i))){

            finalResult.add(first.get(i));
        }
    }


    for (int j = 0; j < second.size(); j++){

        if (!first.contains(second.get(j))){

            finalResult.add(second.get(j));
        }

    }

我刚刚在帖子中描述了两个ArrayLists,我检查了它们的不同元素;如果找到了这样的元素,我将它们添加到finalResult ArrayList。

我希望它会对你有所帮助:)。

答案 3 :(得分:0)

SetList<Integer> A = new SetList<Integer>();
A.addAll({1,2,3,4});

SetList<Integer> B = new SetList<Integer>();
B.addAll({2,3,4,6,7});

Integer a = null;

for (int i=0; i<A.size(); i++)
{
    a = A.get(i);

    if (B.contains(a)
    {
        B.remove(a);
        A.remove(a);
        i--;
    }
}

SetList<Integer> final = new SetList<Integer>();
final.addAll(A);
final.addAll(B);

// final = { 1, 6, 7 }