Java CopyOnWriteArrayList removeAll()方法及示例

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<String> 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<String> 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<String> 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<String> 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程