Java 集合接口的 removeAll() 方法隻刪除指定集合中包含的集合元素。
用法
public boolean removeAll(Collection<?> c)
參數
這裏,參數 'c' 表示包含要從調用的集合中刪除的元素的集合。
返回值:
如果集合因調用而改變,removeAll() 方法返回一個布爾值 'true',否則返回 'false'。
拋出:
UnsupportedOperationException- 如果此集合不支持 removeAll 方法。
ClassCastException- 如果此集合中一個或多個元素的類型與調用的集合不兼容。
NullPointerException- 如果此集合為 null 或者它包含一個或多個 null 元素並且此集合不允許 null 元素。
例子1
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
public class JavaCollectionRemoveAllExample1 {
public static void main(String[] args) {
Collection<Character> collection = new ConcurrentLinkedQueue<Character>();
char i;
for(i='1';i<='9';++i){
collection.add(i);
}
System.out.println("collection:"+collection);
List<Character> list = new ArrayList<Character>();
list.add('2');
list.add('4');
list.add('6');
list.add('8');
System.out.println("List of even numbers:"+list);
//removes all the elements in the collection which are present in list
collection.removeAll(list);
System.out.println("Odd numbers:"+collection);
}
}
輸出:
collection:[1, 2, 3, 4, 5, 6, 7, 8, 9] List of even numbers:[2, 4, 6, 8] Odd numbers:[1, 3, 5, 7, 9]
例子2
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
public class JavaCollectionRemoveAllExample2 {
public static void main(String[] args) {
Collection<Integer> collection = new ConcurrentLinkedQueue<Integer>();
List<Integer> list = new ArrayList<Integer>();
for (int i=1;i<21;i++){
collection.add(i);}
System.out.println("Total no:"+ collection);
for (int i=1;i<21;i++) {
int j=i%2;
if (j != 0) {
list.add(i);
}
}
//removes all the elements int the collection which are present in list
collection.removeAll(list);
System.out.println(" Table of 2:"+collection);
}
}
輸出:
Total no:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Table of 2:[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
例子3
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
public class JavaCollectionRemoveAllExample3 {
public static void main(String[] args) {
Collection<Character> collection = new ConcurrentLinkedQueue<Character>();
char c;
for(c='1';c<='9';++c){
// collection does not support null elements
collection.add(null);
}
System.out.println("collection:"+collection);
List<Character> list = new ArrayList<Character>();
list.add('2');
list.add(null);
list.add('6');
list.add('8');
System.out.println("List of even numbers:"+list);
//removes all the elements in the collection which are present in list
collection.removeAll(list);
System.out.println("Odd numbers:"+collection);
}
}
輸出:
Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.ConcurrentLinkedQueue.checkNotNull(ConcurrentLinkedQueue.java:920) at java.util.concurrent.ConcurrentLinkedQueue.offer(ConcurrentLinkedQueue.java:327) at java.util.concurrent.ConcurrentLinkedQueue.add(ConcurrentLinkedQueue.java:297) at com.javaTpoint.JavaCollectionRemoveAllExample3.main(JavaCollectionRemoveAllExample3.java:13)
相關用法
- Java Collection remove()用法及代碼示例
- Java Collection removeIf()用法及代碼示例
- Java Collection retainAll()用法及代碼示例
- Java Collection toArray()用法及代碼示例
- Java Collection addAll()用法及代碼示例
- Java Collection size()用法及代碼示例
- Java Collection add()用法及代碼示例
- Java Collection equals()用法及代碼示例
- Java Collection hashCode()用法及代碼示例
- Java Collection contains()用法及代碼示例
- Java Collection spliterator()用法及代碼示例
- Java Collection containsAll()用法及代碼示例
- Java Collection clear()用法及代碼示例
- Java Collection isEmpty()用法及代碼示例
- Java Collection iterator()用法及代碼示例
- Java Collections synchronizedSortedSet()用法及代碼示例
- Java Collections checkedQueue()用法及代碼示例
- Java Collections unmodifiableNavigableSet()用法及代碼示例
- Java Collections checkedSet()用法及代碼示例
- Java Collections copy()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Collection removeAll() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。