Java ConcurrentSkipListSet clear()方法
java.util.concurrent.ConcurrentSkipListSet.clear() 方法是Java中的一个内置函数,可以从这个集合中删除所有的元素。
语法
ConcurrentSkipListSet.clear()
参数: 该函数不接受任何参数。
返回值: 该函数不返回任何东西。
下面的程序说明了ConcurrentSkipListSet.clear()方法。
程序1 :
/* Java Program Demonstrate clear()
method of ConcurrentSkipListSet() */
import java.util.concurrent.*;
class ConcurrentSkipListSetClearExample1 {
public static void main(String[] args)
{
// Creating a set object
ConcurrentSkipListSet<Integer> Lset =
new ConcurrentSkipListSet<Integer>();
// Adding elements to this set
for (int i = 10; i <= 50; i += 10)
Lset.add(i);
// Printing elements of the set
System.out.println("The set contains: " + Lset);
// Clear the set
Lset.clear();
// Printing elements of the set after clear operation
System.out.println("After clear operation the set contains: "
+ Lset);
}
}
输出。
The set contains: [10, 20, 30, 40, 50]
After clear operation the set contains: []
程序2
/* Java Program Demonstrate clear()
method of ConcurrentSkipListSet() */
import java.util.concurrent.*;
class ConcurrentSkipListSetClearExample2 {
public static void main(String[] args)
{
// Creating a set object
ConcurrentSkipListSet<String> Lset =
new ConcurrentSkipListSet<String>();
// Adding elements to this set
Lset.add("alex");
Lset.add("bob");
Lset.add("chuck");
Lset.add("drake");
Lset.add("eric");
// Printing elements of the set
System.out.println("The set contains: " + Lset);
// Clear the set
Lset.clear();
// Printing elements of the set after clear operation
System.out.println("After clear operation the set contains: "
+ Lset);
}
}
输出。
The set contains: [alex, bob, chuck, drake, eric]
After clear operation the set contains: []
参考资料: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#clear-