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

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

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

Java .util.concurrent.ConcurrentSkipListMapclone() 方法是Java中的一个内置函数,它返回此 ConcurrentSkipListMap 实例的浅表副本。键和值本身不会被克隆。

句法:

ConcurrentSkipListMap.clone()

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

参数:此方法不接受任何参数。

返回值:该函数返回此 ConcurrentSkipListMap 的浅表副本。

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

方案一:

// 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 mpp
            = new ConcurrentSkipListMap();
  
        // 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
            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}

方案二:

// 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
            mpp = new ConcurrentSkipListMap();
  
        // 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
            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–