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


Java ConcurrentSkipListMap clone()用法及代碼示例


java.util.concurrent.ConcurrentSkipListMap的clone()方法是Java中的內置函數,該函數返回此ConcurrentSkipListMap實例的淺拷貝。鍵和值本身不會被克隆。

用法:

ConcurrentSkipListMap.clone()

參數:該函數不接受任何參數。


參數:此方法不接受任何參數。

返回值:該函數返回此ConcurrentSkipListMap的淺拷貝。

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

示例1:

// Java Program Demonstrate clone() 
// method of ConcurrentSkipListMap 
  
import java.util.concurrent.ConcurrentSkipListMap; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Initializing the map 
        // using ConcurrentSkipListMap() 
        ConcurrentSkipListMap<Integer, Integer> mpp 
            = new ConcurrentSkipListMap<Integer, 
                                        Integer>(); 
  
        // Adding elements to this map 
        mpp.put(1, 1); 
        mpp.put(5, 2); 
        mpp.put(2, 7); 
  
        // Printing the ConcurrentSkipListMap 
        System.out.println("Map: " + mpp); 
  
        // Cloning the ConcurrentSkipListMap 
        ConcurrentSkipListMap<Integer, Integer> 
            clone_mpp = mpp.clone(); 
  
        // Adding elements to the clone map 
        clone_mpp.put(7, 9); 
  
        System.out.println("Cloned map is:  "
                           + clone_mpp); 
    } 
}
輸出:
Map: {1=1, 2=7, 5=2}
Cloned map is:  {1=1, 2=7, 5=2, 7=9}

示例2:

// Java Program Demonstrate clone() 
// method of ConcurrentSkipListMap 
  
import java.util.concurrent.ConcurrentSkipListMap; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Initializing the map 
        // using ConcurrentSkipListMap() 
        ConcurrentSkipListMap<Integer, Integer> 
            mpp = new ConcurrentSkipListMap<Integer, 
                                            Integer>(); 
  
        // Adding elements to this map 
        mpp.put(10, 5); 
        mpp.put(54, 20); 
        mpp.put(2, 7); 
  
        // Printing the ConcurrentSkipListMap 
        System.out.println("Map: " + mpp); 
  
        // Cloning the ConcurrentSkipListMap 
        ConcurrentSkipListMap<Integer, Integer> 
            clone_mpp = mpp.clone(); 
  
        // Adding elements to the clone map 
        clone_mpp.put(17, 9); 
  
        System.out.println("Cloned map is:  "
                           + clone_mpp); 
    } 
}
輸出:
Map: {2=7, 10=5, 54=20}
Cloned map is:  {2=7, 10=5, 17=9, 54=20}

參考: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/ConcurrentSkipListMap.html#clone–



相關用法


注:本文由純淨天空篩選整理自Twinkl Bajaj大神的英文原創作品 ConcurrentSkipListMap clone() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。