Java AbstractSet類的removeAll()方法用於從該集合中刪除所有包含在指定集合中的元素。
用法:
public boolean removeAll(Collection c)
參數:此方法將集合c作為包含要從該集合中刪除的元素的參數。
返回值:如果此集合因調用而更改,則此方法返回true。
異常:該方法引發三種類型的異常:
- UnsupportedOperationException–如果這個集合不支持該操作,則拋出此錯誤。
- ClassCastException–當此集合的元素的類與指定的集合不兼容時,拋出此錯誤。
- NullPointerException –當此集合包含null元素並且指定的集合不允許null元素時,或者如果collection為null,則拋出此錯誤。
以下示例說明了removeAll()方法。
示例1:
// Java program to demonstrate
// removeAll() method for Integer value
import java.util.*;
public class GFG1 {
public static void main(String[] args) throws Exception
{
try {
// Creating object of AbstractSet<Integer>
AbstractSet<Integer>
abs_set = new TreeSet<Integer>();
// Populating abs_set
abs_set.add(1);
abs_set.add(2);
abs_set.add(3);
abs_set.add(4);
abs_set.add(5);
// print abs_set
System.out.println("AbstractSet before "
+ "removeAll() operation : "
+ abs_set);
// Creating another object of ArrayList<Integer>
Collection<Integer>
arrlist2 = new ArrayList<Integer>();
arrlist2.add(1);
arrlist2.add(2);
arrlist2.add(3);
// print arrlist2
System.out.println("Collection Elements"
+ " to be removed : "
+ arrlist2);
// Removing elements from AbstractSet
// specified in arrlist2
// using removeAll() method
abs_set.removeAll(arrlist2);
// print arrlist1
System.out.println("AbstractSet after "
+ "removeAll() operation : "
+ abs_set);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
輸出:
AbstractSet before removeAll() operation : [1, 2, 3, 4, 5] Collection Elements to be removed : [1, 2, 3] AbstractSet after removeAll() operation : [4, 5]
示例2:對於NullPointerException
// Java program to demonstrate
// removeAll() method for Integer value
import java.util.*;
public class GFG1 {
public static void main(String[] args) throws Exception
{
try {
// Creating object of AbstractSet<Integer>
AbstractSet<Integer>
abs_set = new TreeSet<Integer>();
// Populating abs_set
abs_set.add(1);
abs_set.add(2);
abs_set.add(3);
abs_set.add(4);
abs_set.add(5);
// print abs_set
System.out.println("AbstractSet before "
+ "removeAll() operation : "
+ abs_set);
// Creating another object of ArrayList<Integer>
Collection<Integer>
arrlist2 = new ArrayList<Integer>();
arrlist2 = null;
// print arrlist2
System.out.println("Collection Elements"
+ " to be removed : "
+ arrlist2);
// Removing elements from AbstractSet
// specified in arrlist2
// using removeAll() method
abs_set.removeAll(arrlist2);
// print arrlist1
System.out.println("AbstractSet after "
+ "removeAll() operation : "
+ abs_set);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
輸出:
AbstractSet before removeAll() operation : [1, 2, 3, 4, 5] Collection Elements to be removed : null Exception thrown : java.lang.NullPointerException
相關用法
- Java AbstractSet hashCode()用法及代碼示例
- Java AbstractSet equals()用法及代碼示例
- Java Set removeAll()用法及代碼示例
- Java ArrayList removeAll()用法及代碼示例
- Java List removeAll()用法及代碼示例
- Java SortedSet removeAll()用法及代碼示例
- Java CopyOnWriteArraySet removeAll()用法及代碼示例
- Java CopyOnWriteArrayList removeAll()用法及代碼示例
- Java LinkedBlockingDeque removeAll()用法及代碼示例
- Java LinkedTransferQueue removeAll()用法及代碼示例
- Java AbstractSet Class用法及代碼示例
- Java AbstractSet add()用法及代碼示例
- Java AbstractSet contains()用法及代碼示例
- Java AbstractSet toString()用法及代碼示例
- Java AbstractSet size()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 AbstractSet removeAll() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。