Java Hashmap Clone
## Java HashMap clone() Method
The `clone()` method is used to create a shallow copy of a `HashMap` instance.
---
### Understanding Shallow Copy vs. Deep Copy
* **Shallow Copy:** A shallow copy creates a new instance of the `HashMap` object, but it does **not** clone the actual keys and values themselves. Instead, the new map is populated with references to the exact same key and value objects as the original map. If the keys or values are mutable objects, modifying their internal state through one map will affect the other.
* **Deep Copy:** A deep copy creates a completely independent copy of both the map structure and all the key/value objects inside it. In a deep copy, modifying any object in the new map will have absolutely no impact on the original map.
---
### Syntax
The syntax for the `clone()` method is:
```java
hashmap.clone()
```
* `hashmap` is an instance of the `HashMap` class.
### Parameters
* This method does not accept any parameters.
### Return Value
* It returns a shallow copy of the `HashMap` instance as an `Object`. Because it returns type `Object`, you must cast it back to the appropriate `HashMap` type in your code.
---
### Code Examples
#### Example 1: Basic Usage of `clone()`
The following example demonstrates how to clone a `HashMap` and cast the returned object back to the target `HashMap` type.
```java
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// Create a HashMap named 'sites'
HashMap sites = new HashMap<>();
sites.put(1, "Google");
sites.put(2, "YouTip");
sites.put(3, "Taobao");
System.out.println("Original HashMap: " + sites);
// Clone the 'sites' HashMap
@SuppressWarnings("unchecked")
HashMap cloneSites = (HashMap) sites.clone();
System.out.println("Cloned HashMap: " + cloneSites);
}
}
```
**Output:**
```text
Original HashMap: {1=Google, 2=YouTip, 3=Taobao}
Cloned HashMap: {1=Google, 2=YouTip, 3=Taobao}
```
> **Note on Type Casting:**
> The expression `(HashMap) sites.clone()` is used because `clone()` returns a generic `Object`. We cast it to `HashMap` so it can be assigned to the `cloneSites` variable.
---
#### Example 2: Printing the Return Value Directly
You can also print or use the return value of the `clone()` method directly without assigning it to a new variable.
```java
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// Create a HashMap named 'primeNumbers'
HashMap primeNumbers = new HashMap<>();
primeNumbers.put("Two", 2);
primeNumbers.put("Three", 3);
primeNumbers.put("Five", 5);
System.out.println("Numbers: " + primeNumbers);
// Print the return value of the clone() method directly
System.out.println("Return value of clone(): " + primeNumbers.clone());
}
}
```
**Output:**
```text
Numbers: {Five=5, Two=2, Three=3}
Return value of clone(): {Five=5, Two=2, Three=3}
```
---
### Important Considerations
1. **The `Cloneable` Interface:** The `clone()` method is not exclusive to the `HashMap` class. It is defined in the `java.lang.Object` class, and any class that implements the `java.lang.Cloneable` interface can override and use this method.
2. **Shallow Copy Warning:** Remember that if your `HashMap` contains mutable custom objects as keys or values, changing the properties of an object inside the cloned map will also change it in the original map. If you need complete isolation, you must manually perform a deep copy by iterating through the map and cloning each key and value individually.
YouTip