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

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

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

CopyonWriteArraySet 方法的removeAll()方法删除指定集合中存在的此 CopyOnWriteArraySet 的所有元素。这意味着从这个 CopyOnWriteArraySet 中删除两个集合中共有的元素。

句法:

public boolean removeAll(Collection c)

参数:此方法接受一个参数c ,它是包含要从此集合中删除的元素的集合。

返回值:如果 CopyOnWriteArraySet 更改,此方法返回一个布尔值,例如 true。否则此方法返回 false。

异常:此方法抛出以下异常:

  • ClassCastException : 如果这个集合的一个元素的类与指定的集合不兼容
  • NullPointerException :如果此集合包含空元素并且指定的集合不允许空元素,或者指定的集合为空。

下面的程序说明了 CopyOnWriteArrayList 类的 removeAll()函数:

方案一:

// Java Program to illustrate the CopyOnWriteArraySet
// removeall() method in Java
  
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArraySet
        CopyOnWriteArraySet ArrSet
            = new CopyOnWriteArraySet();
  
        // Add elements
        ArrSet.add(10);
        ArrSet.add(20);
        ArrSet.add(30);
        ArrSet.add(50);
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
  
        // create object of ArrayList
        ArrayList Arrlist
            = new ArrayList();
        // add elements
        Arrlist.add(10);
        Arrlist.add(30);
        Arrlist.add(40);
  
        // print ArrayList
        System.out.println("ArrayList: " + Arrlist);
  
        // removing the common elements from
        // the CopyOnWriteArraySet using removeAll()
        System.out.println("Elements removed "
                           + "from CopyOnWriteArraySet: "
                           + ArrSet.removeAll(Arrlist));
  
        // print updated CopyOnWriteArraySet
        System.out.println("Updated CopyOnWriteArraySet: "
                           + ArrSet);
    }
}
输出:
CopyOnWriteArraySet: [10, 20, 30, 50]
ArrayList: [10, 30, 40]
Elements removed from CopyOnWriteArraySet: true
Updated CopyOnWriteArraySet: [20, 50]

程序 2:显示 NullpointerException

// Java Program to illustrate the CopyOnWriteArraySet
// removeall() method in Java
  
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArraySet
        CopyOnWriteArraySet ArrSet
            = new CopyOnWriteArraySet();
  
        // Add elements
        ArrSet.add(10);
        ArrSet.add(20);
        ArrSet.add(30);
        ArrSet.add(50);
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
  
        try {
  
            // removing the null from
            // the CopyOnWriteArraySet using removeAll()
            // This will throw NullPointerException
  
            System.out.println("Elements removed "
                               + "from CopyOnWriteArraySet: "
                               + ArrSet.removeAll(null));
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
CopyOnWriteArraySet: [10, 20, 30, 50]
java.lang.NullPointerException