📌  相关文章
📜  Java中的 ConcurrentSkipListMap clear() 方法及示例

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

Java中的 ConcurrentSkipListMap clear() 方法及示例

Java .util.concurrent.ConcurrentSkipListMapclear()方法是Java中的一个内置函数,它从该映射中删除所有映射。这意味着地图中的所有元素都将被删除并返回一个空地图。

句法:

public void clear()

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

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

下面的程序说明了上述方法:

方案一:

// Java Program Demonstrate clear()
// method of ConcurrentSkipListMap
  
import java.util.concurrent.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Initializing the map
        ConcurrentSkipListMap
            mpp = new ConcurrentSkipListMap();
  
        // 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: {}

方案二:

// Java Program Demonstrate clear()
// method of ConcurrentSkipListMap
  
import java.util.concurrent.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Initializing the map
        ConcurrentSkipListMap
            mpp = new ConcurrentSkipListMap();
  
        // 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: Java/util/concurrent/ConcurrentSkipListMap.html#clear–