CopyOnWriteArrayList类中的removeAll()方法可从调用的CopyOnArrayList对象中删除指定集合中包含的所有元素。
用法:
public boolean removeAll(Collection collection)
参数:该方法仅接受要从调用对象中删除的单个参数集合。
返回值:此方法返回一个布尔值。如果此删除操作成功,则返回true。
异常:此方法引发以下异常:
- ClassCastException:如果此列表的元素的类与指定的集合不兼容。
- NullPointerException :如果指定的集合为null,或者此列表包含null元素,并且指定的集合不允许使用null元素。
以下示例说明了removeAll()方法:
范例1:
// Java program to demonstrate removeAll() method
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
public class Demo {
public static void main(String args[])
{
// Get the CopyOnWriteArrayList
CopyOnWriteArrayList<String> wishlist
= new CopyOnWriteArrayList<>();
// Add the elements in the CopyOnWriteArrayList
wishlist.add("TV");
wishlist.add("computer");
wishlist.add("play station");
wishlist.add("mobile");
wishlist.add("smart watch");
// Print the CopyOnWriteArrayList
System.out.println("CopyOnWriteArrayList:\n"
+ wishlist);
// Get the collection to be removed
ArrayList<String> checkList
= new ArrayList<>();
checkList.add("play station");
checkList.add("TV");
checkList.add("mobile");
System.out.println("\nCollection to be removed:\n"
+ checkList);
// Remove the collection from CopyOnWriteArrayList
// using removeAll() method
wishlist.removeAll(checkList);
// Print the CopyOnWriteArrayList after removal
System.out.println("\nAfter removal of collection"
+ " from CopyOnWriteArrayList:\n"
+ wishlist);
}
}
输出:
CopyOnWriteArrayList: [TV, computer, play station, mobile, smart watch] Collection to be removed: [play station, TV, mobile] After removal of collection from CopyOnWriteArrayList: [computer, smart watch]
范例2:显示NullPointerException
// Java program to demonstrate removeAll() method
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
public class Demo {
public static void main(String args[])
{
// Get the CopyOnWriteArrayList
CopyOnWriteArrayList<String> wishlist
= new CopyOnWriteArrayList<>();
// Add the elements in the CopyOnWriteArrayList
wishlist.add("TV");
wishlist.add("computer");
wishlist.add("play station");
wishlist.add("mobile");
wishlist.add("smart watch");
// Print the CopyOnWriteArrayList
System.out.println("CopyOnWriteArrayList:\n"
+ wishlist);
// Get the collection to be removed
ArrayList<String> checkList
= null;
System.out.println("\nCollection to be removed:"
+ checkList);
try {
// Remove the collection from CopyOnWriteArrayList
// using removeAll() method
wishlist.removeAll(checkList);
}
catch (Exception e) {
// Print the Exception
System.out.println("\nException thrown"
+ " while removing null "
+ "from the CopyOnWriteArrayList:\n"
+ e);
}
}
}
输出:
CopyOnWriteArrayList: [TV, computer, play station, mobile, smart watch] Collection to be removed:null Exception thrown while removing null from the CopyOnWriteArrayList: java.lang.NullPointerException
相关用法
- Java Set removeAll()用法及代码示例
- Java ArrayList removeAll()用法及代码示例
- Java List removeAll()用法及代码示例
- Java LinkedBlockingDeque removeAll()用法及代码示例
- Java LinkedTransferQueue removeAll()用法及代码示例
- Java AbstractSet removeAll()用法及代码示例
- Java CopyOnWriteArraySet removeAll()用法及代码示例
- Java SortedSet removeAll()用法及代码示例
- Java CopyOnWriteArrayList set()用法及代码示例
- Java CopyOnWriteArrayList forEach()用法及代码示例
- Java CopyOnWriteArrayList retainAll()用法及代码示例
- Java CopyOnWriteArrayList remove()用法及代码示例
- Java CopyOnWriteArrayList equals()用法及代码示例
- Java CopyOnWriteArrayList removeIf()用法及代码示例
- Java CopyOnWriteArrayList addAllAbsent()用法及代码示例
注:本文由纯净天空筛选整理自SandySharma大神的英文原创作品 CopyOnWriteArrayList removeAll() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。