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


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


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


相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 AbstractSet removeAll() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。