📜  Java中的HashMap clone()方法

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

Java中的HashMap clone()方法

让我们弄清楚Java中浅拷贝和深拷贝的基本概念。浅层重复更快。但是,它是“惰性的”,它处理指针和引用。它不是创建指针指向的特定知识的当代副本,而是简单地复制指针价格。因此,每个第一个因此副本可以具有引用恒定基础知识的指针。另一方面,深度复制或深度重复真正克隆了底层数据。它不在第一个之间共享,因此在副本之间不共享。

Java.util.HashMap.clone() 方法存在于Java.util 包中,它通常用于返回提到的哈希映射的浅表副本。它只是创建地图的副本。

句法:

Hash_Map.clone()

参数:该方法不带任何参数。

返回值:该方法只返回 HashMap 的一个副本。

示例 1:

Java
// Java Program to Illustrate the clone() Method by
// Mapping String Values to Integer Keys
  
// Importing utility classes
import java.util.*;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating an empty HashMap
        HashMap hash_map
            = new HashMap();
  
        // Mapping string values to int keys
        // Using put() method
  
        // Custom input values passed as arguments
        hash_map.put(10, "Geeks");
        hash_map.put(15, "4");
        hash_map.put(20, "Geeks");
        hash_map.put(25, "Welcomes");
        hash_map.put(30, "You");
  
        // Print and display the HashMap
        System.out.println("Initial Mappings are: "
                           + hash_map);
  
        // Print and display the cloned HashMap
        // using clone() method
        System.out.println("The cloned map look like this: "
                           + hash_map.clone());
    }
}


Java
// Java code to illustrate the clone() method by
// Mapping Integer Values to String Keys
  
// Importing utility classes
import java.util.*;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an empty HashMap
        // Declaring objects of type integer and string
        HashMap hash_map
            = new HashMap();
  
        // Mapping int values to string keys
        // using put() method
        hash_map.put("Geeks", 10);
        hash_map.put("4", 15);
        hash_map.put("Geeks", 20);
        hash_map.put("Welcomes", 25);
        hash_map.put("You", 30);
  
        // Print and display the HashMap
        System.out.println("Initial Mappings are: "
                           + hash_map);
  
        // Print and display the cloned HashMap
        // using clone() method
        System.out.println("The cloned map look like this: "
                           + hash_map.clone());
    }
}


输出:
Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}
The cloned map look like this: {25=Welcomes, 10=Geeks, 20=Geeks, 30=You, 15=4}

示例 2:

Java

// Java code to illustrate the clone() method by
// Mapping Integer Values to String Keys
  
// Importing utility classes
import java.util.*;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an empty HashMap
        // Declaring objects of type integer and string
        HashMap hash_map
            = new HashMap();
  
        // Mapping int values to string keys
        // using put() method
        hash_map.put("Geeks", 10);
        hash_map.put("4", 15);
        hash_map.put("Geeks", 20);
        hash_map.put("Welcomes", 25);
        hash_map.put("You", 30);
  
        // Print and display the HashMap
        System.out.println("Initial Mappings are: "
                           + hash_map);
  
        // Print and display the cloned HashMap
        // using clone() method
        System.out.println("The cloned map look like this: "
                           + hash_map.clone());
    }
}
输出:
Initial Mappings are: {4=15, Geeks=20, You=30, Welcomes=25}
The cloned map look like this: {Geeks=20, 4=15, You=30, Welcomes=25}