当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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–



相关用法


注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 ConcurrentSkipListSet clear() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。