📜  Java中的 ArrayDeque removeAll() 方法

📅  最后修改于: 2022-05-13 01:55:21.217000             🧑  作者: Mango

Java中的 ArrayDeque removeAll() 方法

ArrayDeque 的removeAll()方法用于删除 ArrayDeque 和作为参数传递的集合中共有的所有元素。此方法首先收集 Collection 的所有元素,然后开始从 ArrayDeque 中删除与 Collection 的元素相同的元素。此方法执行后,此 ArrayDequeu 将不包含与该集合共有的元素。如果此 ArrayDeque 由于调用此方法而发生更改,则此方法为 True。

句法:

public boolean removeAll(Collection col)

参数:此方法接受一个参数c ,它表示我们要从该双端队列中删除的元素的集合。

返回:如果由于调用此方法而导致此双端队列发生更改,则此方法返回True

异常:如果指定的集合或其任何元素为空,则此方法抛出NullPointerException

下面的程序说明了 ArrayDeque 的 removeAll() 方法:
程序 1:演示 ArrayDeque 上的 removeAll() 方法的程序,该方法将从包含数字的集合中删除与元素相同的元素。

// Java Program Demonstrate removeAll()
// method of ArrayDeque
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create an ArrayDeque which going to
        // contains a list of Numbers
        ArrayDeque Numbers = new ArrayDeque();
  
        // Add Number to list
        Numbers.add(23);
        Numbers.add(32);
        Numbers.add(45);
        Numbers.add(63);
  
        // print ArrayDeque before calling removeAll()
        System.out.println("Before calling removeAll()");
        print(Numbers);
  
        // create a collection of Number to
        // remove elements from ArrayDeque using removeAll()
        ArrayList col = new ArrayList();
  
        // add Numbers to collection
        col.add(45);
        col.add(44);
        col.add(63);
  
        // remove Numbers same as elements of collection
        // from ArrayDeque using removeAll()
        Numbers.removeAll(col);
  
        // print ArrayDeque after calling removeAll()
        System.out.println("After calling removeAll()");
        print(Numbers);
    }
  
    // printing all elements of ArrayDeque
    public static void print(ArrayDeque arDe)
    {
  
        arDe.forEach((n) -> System.out.print(n + " "));
  
        System.out.println();
    }
}
输出:
Before calling removeAll()
23 32 45 63 
After calling removeAll()
23 32

程序 2:演示 ArrayDeque 上的 removeAll() 方法的程序,该方法将删除与学生姓名集合中的元素相同的元素。

// Java Program Demonstrate removeAll()
// method of ArrayDeque
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create an ArrayDeque which going to
        // contains a list of Student names which is actually
        // string values
        ArrayDeque students = new ArrayDeque();
  
        // Add Strings to list
        // each string represents student name
        students.add("Ram");
        students.add("Mohan");
        students.add("Sohan");
        students.add("Rabi");
  
        // print ArrayDeque before calling removeAll()
        System.out.println("Before calling removeAll()");
        print(students);
  
        // create a collection of Number to
        // remove elements from ArrayDeque using removeAll()
        LinkedList col = new LinkedList();
  
        // add Names in collection
        col.add("Rabi");
        col.add("Sohan");
  
        // remove the elements same as collection
        // from ArrayDeque using removeAll()
        students.removeAll(col);
  
        // print ArrayDeque
        System.out.println("After calling removeAll()");
        print(students);
    }
  
    // printing all elements of ArrayDeque
    public static void print(ArrayDeque arDe)
    {
  
        System.out.println("List of Students Name:");
  
        arDe.forEach((n) -> System.out.print(" | " + n + " | "));
  
        System.out.println("\n");
    }
}
输出:
Before calling removeAll()
List of Students Name:
 | Ram |  | Mohan |  | Sohan |  | Rabi | 

After calling removeAll()
List of Students Name:
 | Ram |  | Mohan |

程序 3:演示 removeAll() 方法抛出的异常的程序。

// Java Program Demonstrate Exception thrown by removeAll()
// method of ArrayDeque
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create an ArrayDeque which going to
        // contains a list of Numbers
        ArrayDeque Numbers = new ArrayDeque();
  
        // Add Number to list
        Numbers.add(223);
        Numbers.add(132);
        Numbers.add(345);
        Numbers.add(563);
  
        // create a collection of Number which is null
        ArrayList col = null;
  
        try {
            // call removeAll() method
            Numbers.removeAll(col);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
java.lang.NullPointerException

参考: https: Java Java.util.Collection)