ArrayDeque的removeAll()方法用於刪除ArrayDeque和作為參數傳遞的集合中共同的所有元素。此方法首先收集Collection的所有元素,然後開始從ArrayDeque中刪除與Collection元素相同的元素。執行此方法後,此ArrayDequeu將不包含與集合相同的元素。如果此ArrayDeque由於調用此方法而發生更改,則此方法為True。
用法:
public boolean removeAll(Collection<? extends E> col)
參數:此方法采用參數c,該參數表示我們要從此雙端隊列刪除的元素的Collection。
返回值:如果此雙端隊列由於調用此方法而發生更改,則此方法返回True。
異常:如果指定的集合或其任何元素為null,則此方法將引發NullPointerException。
以下示例程序旨在說明ArrayDeque的removeAll()方法:
示例1:程序演示ArrayDeque上的removeAll()方法,該方法將從包含Numbers的集合中刪除與其元素相同的元素。
// 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<Integer> Numbers = new ArrayDeque<Integer>();
// 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<Integer> col = new ArrayList<Integer>();
// 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<Integer> 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<String> students = new ArrayDeque<String>();
// 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<String> col = new LinkedList<String>();
// 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<String> 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<Integer> Numbers = new ArrayDeque<Integer>();
// 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<Integer> col = null;
try {
// call removeAll() method
Numbers.removeAll(col);
}
catch (Exception e) {
System.out.println(e);
}
}
}
java.lang.NullPointerException
參考: https://docs.oracle.com/javase/10/docs/api/java/util/ArrayDeque.html#removeAll(java.util.Collection)
相關用法
- Java HashSet removeAll()用法及代碼示例
- Java TreeSet removeAll()用法及代碼示例
- Java ConcurrentSkipListSet removeAll()用法及代碼示例
- Java Stack removeAll()用法及代碼示例
- Java Set removeAll()用法及代碼示例
- Java Vector removeAll()用法及代碼示例
- Java AbstractCollection removeAll()用法及代碼示例
- Java AbstractSequentialList removeAll()用法及代碼示例
- Java LinkedHashSet removeAll()用法及代碼示例
- Java AbstractSet removeAll()用法及代碼示例
- Java LinkedBlockingDeque removeAll()用法及代碼示例
- Java List removeAll()用法及代碼示例
- Java LinkedTransferQueue removeAll()用法及代碼示例
- Java CopyOnWriteArraySet removeAll()用法及代碼示例
- Java SortedSet removeAll()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ArrayDeque removeAll() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。