Home » Java programming language

Java IdentityHashMap clone() Method with Example

IdentityHashMap Class clone() method: Here, we are going to learn about the clone() method of IdentityHashMap Class with its syntax and example.
Submitted by Preeti Jain, on March 06, 2020

IdentityHashMap Class clone() method

  • clone() method is available in java.util package.
  • clone() method is used to return a shallow copy of this IdentityHashMap.
  • clone() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • clone() method does not throw an exception at the time of cloning an object.

Syntax:

    public Object clone();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is Object, it returns cloned copy of this IdentityHashMap.

Example:

// Java program to demonstrate the example 
// of Object clone() method of IdentityHashMap 

import java.util.*;

public class CloneOfIdentityHashMap {
    public static void main(String[] args) {
        // Instantiates a IdentityHashMap object
        IdentityHashMap < Integer, String > ihm = new IdentityHashMap < Integer, String > ();
        IdentityHashMap < Integer, String > clone_map = new IdentityHashMap < Integer, String > ();

        // By using put() method is to add
        // key-value pairs in a IdentityHashMap
        ihm.put(10, "C");
        ihm.put(20, "C++");
        ihm.put(50, "JAVA");
        ihm.put(40, "PHP");
        ihm.put(30, "SFDC");

        // Display IdentityHashMap and clone_map
        System.out.println("IdentityHashMap: " + ihm);
        System.out.println("CloneMap: " + clone_map);

        // By using clone() method is to clone
        // this object

        clone_map = (IdentityHashMap) ihm.clone();

        // Display clone_map
        System.out.println("ihm.clone(): " + clone_map);
    }
}

Output

IdentityHashMap: {20=C++, 40=PHP, 50=JAVA, 30=SFDC, 10=C}
CloneMap: {}
ihm.clone(): {20=C++, 40=PHP, 50=JAVA, 30=SFDC, 10=C}


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.