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


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