Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

how to create a clone of a HashMap using the collections in java

how to create a clone of a HashMap using the collections in java

Previous Home Next

 

In this example we are creating the clone of a HashMap using collections.

For creating a HashMap first of all we have to import the java.util package in which the java.util.HashMap class is contained which is used for creating the HashMap. In this example we have created a class named CollectionExample in which we have created HashMap and added some elements to it.
 In this example we have used the clone() method on the object of the HashMap for creating the clone of the HashMap. The syntax of the clone() in general is ,

Object clone()
This method returns a shallow copy of the HashMap instance.

In this example we have created a HashMap which contains String keys and values of integral types. We have added some elements this HashMap object hm by using the put(Object key, Object value) method it. 
We have also created Hashtable and added all the elements of it to the HashMap by using the putAll(Map map) method.
We have created another HashMap named cloneMap of same type as HashMap hm. The values returned by invoking the clone() method on the object of the HashMap hm are stored in this cloneMap. Finally we have displayed the values this cloneMap via using Iterator interface.

 
package r4r.co.in;
import java.util.*;


public class CollectionExample {

/**
* @param args
*/
public static void main(String args[]) throws IllegalArgumentException, NullPointerException
{
try
{
HashMap<String, Integer> hm = new HashMap<String, Integer>(); // creating a HashMap having keys in string format and values in Integer
hm.put("one-> ", 1); // adding values to the HashMap
hm.put("two-> ", 2);
hm.put("three-> ", 3);
hm.put("four-> ", 4);
hm.put("five-> ", 5);
System.out.println("The key contained in the HashMap are as follows: ");
Iterator<String> itr=hm.keySet().iterator(); // Invoking iterator on the key set of the hashmap
Iterator<Integer> itr1=hm.values().iterator(); // Invoking iterator on the value set of the hashmap
while((itr.hasNext())&&(itr1.hasNext()))
{
System.out.print(itr.next()); // displaying the keys of the hashmap
System.out.print(itr1.next()); // displaying the values of the hashmap
System.out.println();
}
Hashtable<String, Integer> htbl=new Hashtable<String, Integer>(); // creating a Hashtable named htbl
htbl.put("five->", 5); // adding values to the hashtable
htbl.put("six-> ", 6);
htbl.put("seven-> ", 7);
htbl.put("eight-> ", 8);
htbl.put("nine-> ", 9);
System.out.println("The contents of the created Hashtable htbl are: ");
itr=htbl.keySet().iterator(); // Invoking iterator on the keyset of the hashtable
itr1=htbl.values().iterator(); // Invoking iterator on the values set of the hashtable
while((itr.hasNext())&&(itr1.hasNext()))
{
System.out.print(itr.next()); // displaying the keys of the hashtable
System.out.print(itr1.next()); // displaying the values of the hashtable
System.out.println();

}
hm.putAll(htbl); // adding the Hashtable collection to the HashMap
System.out.println("Now the contents of the HashMap after adding all the contents of the Hashtable are: ");
itr=hm.keySet().iterator(); // Invoking the iterator on the keyset of the resulted HashMap
itr1=hm.values().iterator(); // invoking the iterator on the value set of the resulted HashMap
while((itr.hasNext())&&(itr1.hasNext()))
{
System.out.print(itr.next()); // diplaying the keys of the resulted HashMap
System.out.print(itr1.next()); // displaying the values of the resulted HashMap
System.out.println();

}

HashMap<String, Integer> cloneMap=new HashMap<String, Integer>(); // creating an empty hashMap named cloneMap
cloneMap=(HashMap<String, Integer>)hm.clone(); // creating a clone of the HashMap hm and storing it to cloneMap
System.out.println("The contents of the cloned HashMap are as follows: ");
itr=cloneMap.keySet().iterator(); // Invoking the iterator on the keyset of the cloneMap HashMap
itr1=cloneMap.values().iterator(); // invoking the iterator on the value set of the cloneMap HashMap
while((itr.hasNext())&&(itr1.hasNext()))
{
System.out.print(itr.next()); // diplaying the keys of the cloneMap HashMap
System.out.print(itr1.next()); // displaying the values of the cloneMap HashMap
System.out.println();

}

}

catch(NullPointerException e)
{
e.printStackTrace();
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
}


}
The output of the above written example is as follows:

The key contained in the HashMap are as follows:
two-> 2
one-> 1
five-> 5
three-> 3
four-> 4
The contents of the created Hashtable htbl are:
nine-> 9
six-> 6
eight-> 8
seven-> 7
five->5
Now the contents of the HashMap after adding all the contents of the Hashtable are:
eight-> 8
six-> 6
nine-> 9
two-> 2
one-> 1
seven-> 7
five-> 5
three-> 3
five->5
four-> 4
The contents of the cloned HashMap are as follows:
eight-> 8
two-> 2
nine-> 9
six-> 6
one-> 1
seven-> 7
three-> 3
five-> 5
five->5
four-> 4

Previous Home Next