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


Java ConcurrentSkipListMap clear()用法及代码示例


java.util.concurrent.ConcurrentSkipListMap的clear()方法是Java中的内置函数,该函数从此映射中删除了所有映射。这意味着将删除映射中的所有元素,并返回一个空映射。

用法:

public void clear()

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


返回值:该函数从该映射中删除所有映射。

以下示例程序旨在说明上述方法:

示例1:

// Java Program Demonstrate clear() 
// method of ConcurrentSkipListMap 
  
import java.util.concurrent.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Initializing the map 
        ConcurrentSkipListMap<Integer, Integer> 
            mpp = new ConcurrentSkipListMap<Integer, 
                                            Integer>(); 
  
        // adding elements in map 
        for (int i = 1; i <= 5; i++) 
            mpp.put(i, i); 
  
        // print original map 
        System.out.println("map elements are: "
                           + mpp); 
  
        mpp.clear(); 
  
        // after clear() operation 
        System.out.println("after clear the map is: "
                           + mpp); 
    } 
}
输出:
map elements are: {1=1, 2=2, 3=3, 4=4, 5=5}
after clear the map is: {}

示例2:

// Java Program Demonstrate clear() 
// method of ConcurrentSkipListMap 
  
import java.util.concurrent.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Initializing the map 
        ConcurrentSkipListMap<Integer, Integer> 
            mpp = new ConcurrentSkipListMap<Integer, 
                                            Integer>(); 
  
        // adding elements in map 
        for (int i = 1; i <= 10; i++) 
            mpp.put(i, i); 
  
        // print original map 
        System.out.println("map elements are: "
                           + mpp); 
  
        mpp.clear(); 
  
        // after clear() operation 
        System.out.println("after clear the map is: "
                           + mpp); 
    } 
}
输出:
map elements are: {1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10}
after clear the map is: {}

参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListMap.html#clear–



相关用法


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