當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。