Método ConcurrentSkipListSet clone() en Java

El método clone() de java.util.concurrent.ConcurrentSkipListSet es una función integrada en Java que devuelve una copia superficial de esta instancia de ConcurrentSkipListSet.

Sintaxis:

ConcurrentSkipListSet.clone()

Parámetros: La función no acepta ningún parámetro.

Valor devuelto: la función devuelve una copia superficial de ConcurrentSkipListSet.

Los siguientes programas ilustran el método ConcurrentSkipListSet.clone():

Programa 1:

// Java Program Demonstrate clone()
// method of ConcurrentSkipListSet
import java.util.concurrent.*;
  
class ConcurrentSkipListSetCloneExample1 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet<Integer>
            set = new ConcurrentSkipListSet<Integer>();
  
        // 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<Integer> 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);
    }
}
Producción:

The set contains: [10, 11, 12, 13, 14, 15]
The clone set contains: [10, 11, 12, 13, 14, 15, 16]

Programa 2:

// Java Program Demonstrate clone()
// method of ConcurrentSkipListSet */
import java.util.concurrent.*;
  
class ConcurrentSkipListSetCloneExample2 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet<String>
            set = new ConcurrentSkipListSet<String>();
  
        // 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<String> 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);
    }
}
Producción:

The set contains: [Gfg, fun, is]
The clone set contains: [Gfg, fun, is, site]

Referencia : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#clone()

Publicación traducida automáticamente

Artículo escrito por rupesh_rao y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *