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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。