📌  相关文章
📜  Java中的 CopyOnWriteArrayList removeAll() 方法及示例

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

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

CopyOnWriteArrayList 类中的removeAll()方法从您调用的 CopyOnArrayList 对象中删除指定集合中包含的所有元素。

句法:

public boolean removeAll(Collection collection)

参数:该方法仅接受要从调用对象中删除的单个参数集合

返回值:此方法返回一个布尔值。如果此删除操作成功,则返回 true。

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

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

下面的示例说明了 removeAll() 方法:

示例 1:

// Java program to demonstrate removeAll() method
  
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
  
public class Demo {
  
    public static void main(String args[])
    {
  
        // Get the CopyOnWriteArrayList
        CopyOnWriteArrayList wishlist
            = new CopyOnWriteArrayList<>();
  
        // Add the elements in the CopyOnWriteArrayList
        wishlist.add("TV");
        wishlist.add("computer");
        wishlist.add("play station");
        wishlist.add("mobile");
        wishlist.add("smart watch");
  
        // Print the CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList: \n"
                           + wishlist);
  
        // Get the collection to be removed
        ArrayList checkList
            = new ArrayList<>();
  
        checkList.add("play station");
        checkList.add("TV");
        checkList.add("mobile");
  
        System.out.println("\nCollection to be removed:\n"
                           + checkList);
  
        // Remove the collection from CopyOnWriteArrayList
        // using removeAll() method
        wishlist.removeAll(checkList);
  
        // Print the CopyOnWriteArrayList after removal
        System.out.println("\nAfter removal of collection"
                           + " from CopyOnWriteArrayList:\n"
                           + wishlist);
    }
}
输出:
CopyOnWriteArrayList: 
[TV, computer, play station, mobile, smart watch]

Collection to be removed:
[play station, TV, mobile]

After removal of collection from CopyOnWriteArrayList:
[computer, smart watch]

示例 2:显示 NullPointerException

// Java program to demonstrate removeAll() method
  
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
  
public class Demo {
  
    public static void main(String args[])
    {
  
        // Get the CopyOnWriteArrayList
        CopyOnWriteArrayList wishlist
            = new CopyOnWriteArrayList<>();
  
        // Add the elements in the CopyOnWriteArrayList
        wishlist.add("TV");
        wishlist.add("computer");
        wishlist.add("play station");
        wishlist.add("mobile");
        wishlist.add("smart watch");
  
        // Print the CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList: \n"
                           + wishlist);
  
        // Get the collection to be removed
        ArrayList checkList
            = null;
        System.out.println("\nCollection to be removed: "
                           + checkList);
  
        try {
  
            // Remove the collection from CopyOnWriteArrayList
            // using removeAll() method
            wishlist.removeAll(checkList);
        }
        catch (Exception e) {
  
            // Print the Exception
            System.out.println("\nException thrown"
                               + " while removing null "
                               + "from the CopyOnWriteArrayList: \n"
                               + e);
        }
    }
}
输出:
CopyOnWriteArrayList: 
[TV, computer, play station, mobile, smart watch]

Collection to be removed: null

Exception thrown while removing null from the CopyOnWriteArrayList: 
java.lang.NullPointerException