当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Collection removeAll()用法及代码示例


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