📜  Java中的 AbstractSet removeAll() 方法及示例

📅  最后修改于: 2022-05-13 01:54:24.275000             🧑  作者: Mango

Java中的 AbstractSet removeAll() 方法及示例

Java AbstractSet类的removeAll()方法用于从该集合中删除指定集合中包含的所有元素。

句法:

public boolean removeAll(Collection c)

参数:此方法将集合 c作为包含要从此集合中删除的元素的参数。

返回值:如果此集合因调用而更改,则此方法返回true

异常:该方法抛出三种类型的异常:

  • UnsupportedOperationException – 如果此集合不支持该操作,则抛出此异常。
  • ClassCastException – 当此集合的元素的类与指定的集合不兼容时,将引发此异常。
  • NullPointerException – 当此集合包含空元素并且指定的集合不允许空元素,或者如果集合为空时,将引发此异常。

下面是说明removeAll()方法的示例。

方案一:

// 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
            AbstractSet
                abs_set = new TreeSet();
  
            // 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
            Collection
                arrlist2 = new ArrayList();
            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
            AbstractSet
                abs_set = new TreeSet();
  
            // 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
            Collection
                arrlist2 = new ArrayList();
            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