當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java CopyOnWriteArraySet removeAll()用法及代碼示例


CopyonWriteArraySet方法的removeAll()方法刪除指定集合中存在的該CopyOnWriteArraySet的所有元素。這意味著從此CopyOnWriteArraySet中刪除兩個集合中共有的元素。

用法:

public boolean removeAll(Collection<E> c)

參數:此方法接受參數c,該參數c是包含要從此集合中刪除的元素的集合。


返回值:如果CopyOnWriteArraySet更改,則此方法返回布爾值,例如true。否則,此方法返回false。

異常:此方法引發以下異常:

  • ClassCastException:如果此集合的元素的類與指定的集合不兼容
  • NullPointerException :如果此集合包含null元素,並且指定的集合不允許null元素,或者指定的collection為null。

以下示例程序旨在說明CopyOnWriteArrayList類的removeAll()函數:

示例1:

// 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<Integer> ArrSet 
            = new CopyOnWriteArraySet<Integer>(); 
  
        // 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<Integer> Arrlist 
            = new ArrayList<Integer>(); 
        // 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<Integer> ArrSet 
            = new CopyOnWriteArraySet<Integer>(); 
  
        // 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


相關用法


注:本文由純淨天空篩選整理自ProgrammerAnvesh大神的英文原創作品 CopyOnWriteArraySet removeAll() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。