📌  相关文章
📜  Java中的 ConcurrentSkipListSet clear() 方法

📅  最后修改于: 2022-05-13 01:55:43.010000             🧑  作者: Mango

Java中的 ConcurrentSkipListSet clear() 方法

Java .util.concurrent.ConcurrentSkipListSet.clear()方法是Java中的一个内置函数,它从该集合中删除所有元素。

句法:

ConcurrentSkipListSet.clear()

参数:该函数不接受任何参数。

返回值:该函数不返回任何内容。

下面的程序说明了 ConcurrentSkipListSet.clear() 方法:

方案一:

/* Java Program Demonstrate clear()
method of ConcurrentSkipListSet() */
import java.util.concurrent.*;
class ConcurrentSkipListSetClearExample1 {
    public static void main(String[] args)
    {
        // Creating a set object
        ConcurrentSkipListSet Lset = 
                   new ConcurrentSkipListSet();
  
        // 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: []

方案二:

/* Java Program Demonstrate clear()
method of ConcurrentSkipListSet() */
import java.util.concurrent.*;
class ConcurrentSkipListSetClearExample2 {
    public static void main(String[] args)
    {
        // Creating a set object
        ConcurrentSkipListSet Lset = 
                    new ConcurrentSkipListSet();
  
        // 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: Java/util/concurrent/ConcurrentSkipListSet.html#clear–