java removeall 对象_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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值