Java.util.AbstractCollection.removeAll(Collection col) 方法用於從 AbstractCollection 中刪除指定集合中存在的所有元素。
用法:
AbstractCollection.removeAll(Collection col)
參數:此方法接受一個強製參數 col,該參數是要從 AbstractCollection 中刪除其元素的集合。
返回值:如果 AbstractCollection 完全由於操作而改變,則此方法返回 true,否則返回 False。
異常:如果指定的集合為空,該方法將拋出 NullPointerException。
以下示例程序旨在說明 Java.util.AbstractCollection.removeAll(Collection col) 方法:
程序1:
// Java code to illustrate removeAll()
import java.util.*;
public class AbstractCollectionDemo {
public static void main(String args[])
{
// Creating an empty AbstractCollection
AbstractCollection<String> collection
= new ArrayList<String>();
// Use add() method to add
// elements in the AbstractCollection
collection.add("Geeks");
collection.add("for");
collection.add("Geeks");
collection.add("10");
collection.add("20");
// Output the AbstractCollection
System.out.println("AbstractCollection:"
+ collection);
// Creating an empty AbstractCollection
AbstractCollection<String> colcollection
= new ArrayList<String>();
// Use add() method to add
// elements in the AbstractCollection
colcollection.add("Geeks");
colcollection.add("for");
colcollection.add("Geeks");
// Remove the head using remove()
boolean changed
= collection.removeAll(colcollection);
// Print the result
if (changed)
System.out.println("Collection removed");
else
System.out.println("Collection not removed");
// Print the final AbstractCollection
System.out.println("Final AbstractCollection:"
+ collection);
}
}
輸出:
AbstractCollection:[Geeks, for, Geeks, 10, 20] Collection removed Final AbstractCollection:[10, 20]
程序2:
// Java code to illustrate removeAll()
import java.util.*;
public class AbstractCollectionDemo {
public static void main(String args[])
{
// Creating an empty AbstractCollection
AbstractCollection<Integer> collection
= new ArrayList<Integer>();
// Use add() method to
// add elements in the AbstractCollection
collection.add(1);
collection.add(2);
collection.add(3);
collection.add(10);
collection.add(20);
// Output the AbstractCollection
System.out.println("AbstractCollection:"
+ collection);
// Creating an empty AbstractCollection
AbstractCollection<Integer> colcollection
= new ArrayList<Integer>();
// Use add() method to add elements
// in the AbstractCollection
colcollection.add(30);
colcollection.add(40);
colcollection.add(50);
// Remove the head using remove()
boolean changed
= collection.removeAll(colcollection);
// Print the result
if (changed)
System.out.println("Collection removed");
else
System.out.println("Collection not removed");
// Print the final AbstractCollection
System.out.println("Final AbstractCollection:"
+ collection);
}
}
輸出:
AbstractCollection:[1, 2, 3, 10, 20] Collection not removed Final AbstractCollection:[1, 2, 3, 10, 20]
相關用法
- Java AbstractSequentialList removeAll()用法及代碼示例
- Java LinkedHashSet removeAll()用法及代碼示例
- Java Stack removeAll()用法及代碼示例
- Java TreeSet removeAll()用法及代碼示例
- Java HashSet removeAll()用法及代碼示例
- Java AbstractCollection clear()用法及代碼示例
- Java AbstractCollection addAll()用法及代碼示例
- Java AbstractCollection add()用法及代碼示例
- Java AbstractCollection isEmpty()用法及代碼示例
- Java AbstractCollection size()用法及代碼示例
- Java AbstractCollection remove()用法及代碼示例
- Java AbstractCollection contains()用法及代碼示例
- Java AbstractCollection containsAll()用法及代碼示例
- Java AbstractCollection toArray()用法及代碼示例
- Java AbstractCollection retainAll()用法及代碼示例
- Java AbstractCollection toString()用法及代碼示例
- Java AbstractCollection用法及代碼示例
- Java Vector removeAll()用法及代碼示例
- Java ArrayDeque removeAll()用法及代碼示例
- Java ConcurrentSkipListSet removeAll()用法及代碼示例
- Java ArrayList removeAll()用法及代碼示例
- Java AbstractSet removeAll()用法及代碼示例
- Java CopyOnWriteArrayList removeAll()用法及代碼示例
- Java Set removeAll()用法及代碼示例
注:本文由純淨天空篩選整理自AnmoldeepKaur大神的英文原創作品 AbstractCollection removeAll() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。