📜  Java中的 ConcurrentSkipListSet clone() 方法

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

Java中的 ConcurrentSkipListSet clone() 方法

Java .util.concurrent.ConcurrentSkipListSetclone()方法是Java中的一个内置函数,它返回此 ConcurrentSkipListSet 实例的浅表副本。

句法:

ConcurrentSkipListSet.clone()

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

返回值:函数返回 ConcurrentSkipListSet 的浅拷贝。

下面的程序说明了 ConcurrentSkipListSet.clone() 方法:

方案一:

// Java Program Demonstrate clone()
// method of ConcurrentSkipListSet
import java.util.concurrent.*;
  
class ConcurrentSkipListSetCloneExample1 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet
            set = new ConcurrentSkipListSet();
  
        // Adding elements to this set
        for (int i = 10; i <= 15; i++)
            set.add(i);
  
        // Printing elements of the set
        System.out.println("The set contains: " + set);
  
        // Cloning the ConcurrentSkipListSet
        ConcurrentSkipListSet clone_set = set.clone();
  
        // Adding elements to the clone set
        clone_set.add(16);
  
        // Printing elements of the clone set
        System.out.println("The clone set contains: " + clone_set);
    }
}
输出:
The set contains: [10, 11, 12, 13, 14, 15]
The clone set contains: [10, 11, 12, 13, 14, 15, 16]

方案二:

// Java Program Demonstrate clone()
// method of ConcurrentSkipListSet */
import java.util.concurrent.*;
  
class ConcurrentSkipListSetCloneExample2 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet
            set = new ConcurrentSkipListSet();
  
        // Adding elements to this set
        set.add("Gfg");
        set.add("is");
        set.add("fun");
  
        // Printing elements of the set
        System.out.println("The set contains: " + set);
  
        // Cloning the ConcurrentSkipListSet
        ConcurrentSkipListSet clone_set = set.clone();
  
        // Adding elements to the clone set
        clone_set.add("site");
  
        // Printing elements of the clone set
        System.out.println("The clone set contains: " + clone_set);
    }
}
输出:
The set contains: [Gfg, fun, is]
The clone set contains: [Gfg, fun, is, site]

参考:https: Java/util/concurrent/ConcurrentSkipListSet.html#clone()