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


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


CopyOnWriteArrayList類中的removeAll()方法可從調用的CopyOnArrayList對象中刪除指定集合中包含的所有元素。

用法:

public boolean removeAll(Collection collection)

參數:該方法僅接受要從調用對象中刪除的單個參數集合。


返回值:此方法返回一個布爾值。如果此刪除操作成功,則返回true。

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

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

以下示例說明了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


相關用法


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