java.util.concurrent.ConcurrentSkipListSet的removeAll()方法是Java中的一个内置函数,该函数从该集合中移除指定集合中包含的所有元素。如果指定的集合也是一个集合,则此操作会有效地修改此集合,以使其值为两个集合的非对称集合差异。
用法:
public boolean removeAll(Collection> c)
参数:该函数接受单个参数c
返回值:如果此设置由于调用而改变,则函数返回true。
异常:该函数引发以下异常:
- ClassCastException–如果该集合中一个或多个元素的类型与指定的集合不兼容
- NullPointerException –如果指定的集合或其任何元素为空
以下示例程序旨在说明ConcurrentSkipListSet.removeAll()方法:
示例1:
// Java program to demonstrate removeAll()
// method of ConcurrentSkipListSet
import java.util.concurrent.*;
import java.util.ArrayList;
import java.util.List;
class ConcurrentSkipListSetremoveAllExample1 {
public static void main(String[] args)
{
// Initializing the List
List<Integer> list = new ArrayList<Integer>();
// Adding elements in the list
for (int i = 1; i <= 10; i += 2)
list.add(i);
// Contents of the list
System.out.println("Contents of the list: " + list);
// Initializing the set
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
// Adding elements in the set
for (int i = 1; i <= 10; i++)
set.add(i);
// Contents of the set
System.out.println("Contents of the set: " + set);
// Remove all elements from the set which are in the list
set.removeAll(list);
// Contents of the set after removal
System.out.println("Contents of the set after removal: "
+ set);
}
}
输出:
Contents of the list: [1, 3, 5, 7, 9] Contents of the set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Contents of the set after removal: [2, 4, 6, 8, 10]
示例2:在removeAll()中显示NullPOinterException的程序。
// Java program to demonstrate removeAll()
// method of ConcurrentSkipListSet
import java.util.concurrent.*;
import java.util.ArrayList;
import java.util.List;
class ConcurrentSkipListSetremoveAllExample1 {
public static void main(String[] args)
{
// Initializing the set
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
// Adding elements in the set
for (int i = 1; i <= 10; i++)
set.add(i);
// Contents of the set
System.out.println("Contents of the set: " + set);
try {
// Remove all elements from the set which are null
set.removeAll(null);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出:
Contents of the set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Exception: java.lang.NullPointerException
参考:
相关用法
- Java ArrayDeque removeAll()用法及代码示例
- Java LinkedHashSet removeAll()用法及代码示例
- Java TreeSet removeAll()用法及代码示例
- Java Set removeAll()用法及代码示例
- Java HashSet removeAll()用法及代码示例
- Java Stack removeAll()用法及代码示例
- Java Vector removeAll()用法及代码示例
- Java AbstractSequentialList removeAll()用法及代码示例
- Java AbstractCollection removeAll()用法及代码示例
- Java ConcurrentSkipListSet contains()用法及代码示例
- Java ConcurrentSkipListSet add()用法及代码示例
- Java ConcurrentSkipListSet last()用法及代码示例
- Java ConcurrentSkipListSet first()用法及代码示例
- Java SortedSet removeAll()用法及代码示例
- Java AbstractSet removeAll()用法及代码示例
注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 ConcurrentSkipListSet removeAll() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。