移除元素向量 java

Remove element vector java

我有 2 个向量,其元素如下:

vect 1 = [111111 5, 111111 5, 222222 5, 333333 5, 111111 2]
vect 2 = [111111 5, 222222 4, 333333 2, 111111 2, 444444 8, 333333 5, 111111 1, 222222 5]

我如何在 Java 中移除 vect 2 中存在的 vector 1 的元素?

我想得到这个结果:

vect 2 = [222222 4, 333333 2, 444444 8, 111111 1]

感谢

使用collToRemoveFrom.removeAll(collection);

您可以使用Collection的方法removeAll(Collection<?> c)。这适用于任何 Collection.

因此您可以执行以下操作:

List v1 = ....
List v2 = ....
v2.removeAll(v1); // Now v2 contains only elements of original v2 not present in v1

试试 Vector 的 removeAll 方法,

public static void main(String[] args) {
    Vector v1 = new Vector();
    Vector v2 = new Vector();

    v1.add(1111);
    v2.add(1111);
    v2.add(2222);

    v2.removeAll(v1);
    System.out.println(v2);

}