Java LinkedHashMap clone() Method

The LinkedHashMap.clone() method in Java is used to create a shallow copy of a LinkedHashMap.

Table of Contents

  1. Introduction
  2. clone Method Syntax
  3. Examples
    • Cloning a LinkedHashMap
  4. Real-World Use Case
    • Example: Creating Backup of Configuration Settings
  5. Conclusion

Introduction

The LinkedHashMap.clone() method is a member of the LinkedHashMap class in Java. It allows you to create a shallow copy of a LinkedHashMap. This can be useful when you need to duplicate the map while preserving the insertion order and without affecting the original map.

clone() Method Syntax

The syntax for the clone method is as follows:

public Object clone()
  • The method does not take any parameters.
  • The method returns a shallow copy of the LinkedHashMap.

Examples

Cloning a LinkedHashMap

The clone method can be used to create a shallow copy of a LinkedHashMap.

Example

import java.util.LinkedHashMap;

public class CloneExample {
    public static void main(String[] args) {
        // Creating a LinkedHashMap with String keys and Integer values
        LinkedHashMap<String, Integer> people = new LinkedHashMap<>();

        // Adding entries to the LinkedHashMap
        people.put("Ravi", 25);
        people.put("Priya", 30);
        people.put("Vijay", 35);

        // Cloning the LinkedHashMap
        LinkedHashMap<String, Integer> clonedPeople = (LinkedHashMap<String, Integer>) people.clone();

        // Printing the original and cloned LinkedHashMap
        System.out.println("Original LinkedHashMap: " + people);
        System.out.println("Cloned LinkedHashMap: " + clonedPeople);
    }
}

Output:

Original LinkedHashMap: {Ravi=25, Priya=30, Vijay=35}
Cloned LinkedHashMap: {Ravi=25, Priya=30, Vijay=35}

Real-World Use Case

Example: Creating Backup of Configuration Settings

A common real-world use case for LinkedHashMap.clone() is creating a backup of configuration settings in an application. For example, let's consider a scenario where we have a LinkedHashMap storing configuration settings, and we want to create a backup before making changes.

Example

import java.util.LinkedHashMap;
import java.util.Map;

public class ConfigurationManager {
    public static void main(String[] args) {
        // Creating a LinkedHashMap to store configuration settings
        LinkedHashMap<String, String> configSettings = new LinkedHashMap<>();

        // Adding configuration settings
        configSettings.put("server", "localhost");
        configSettings.put("port", "8080");
        configSettings.put("timeout", "30");

        // Printing the original configuration settings
        System.out.println("Original Configuration Settings: ");
        for (Map.Entry<String, String> entry : configSettings.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        // Cloning the configuration settings to create a backup
        LinkedHashMap<String, String> backupSettings = (LinkedHashMap<String, String>) configSettings.clone();

        // Making changes to the original configuration settings
        configSettings.put("timeout", "60");

        // Printing the updated and backup configuration settings
        System.out.println("\nUpdated Configuration Settings: ");
        for (Map.Entry<String, String> entry : configSettings.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        System.out.println("\nBackup Configuration Settings: ");
        for (Map.Entry<String, String> entry : backupSettings.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

Output:

Original Configuration Settings:
server: localhost
port: 8080
timeout: 30

Updated Configuration Settings:
server: localhost
port: 8080
timeout: 60

Backup Configuration Settings:
server: localhost
port: 8080
timeout: 30

In this example, LinkedHashMap.clone() is used to create a backup of the configuration settings before making changes, ensuring that the original settings can be restored if needed.

Conclusion

The LinkedHashMap.clone() method in Java provides a way to create a shallow copy of a LinkedHashMap. By understanding how to use this method, you can efficiently duplicate collections of key-value pairs in your Java applications while preserving the insertion order. The method allows you to create backups and work with copies of the map without affecting the original, making it a versatile tool for data management.

Comments