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 LinkedHashMap using collections in java

how to create a clone of LinkedHashMap using collections in java

Previous Home Next

 

In this example we shall create a clone of  the LinkedHashMap.

For creating a LinkedHashMap first of all we have to import the java.util package in which the java.util.LinkedHashMap class is contained. In this example we have created a class named CollectionExample in which we have created a LinkedHashMap and added some elements to it
 In the following example we have used the clone() method for creating the clone the LinkedHashMap. The general syntax of the clone() method is,
 Object clone()
This method returns a shallow copy of the LinkedHashMap instance.

In this example we have created an object of the LinkedHashMap lhm and added some elements to the LinkedHashMap by using put(Object key, Object value) method on its object. Then we have displayed the contents of the LinkedHashMap.
We have created another LinkedHashMap named cloneMap and then stored the values returned by invoking the clone() method on the object of the LinkedHashMap lhm in the cloneMap.
We have created a Set of the values of the cloneMap and then invoked the iterator on the object of the Set set and by using the Map.Entry Interface we have displayed the contents of the cloneMap.

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


public class CollectionExample {

/**
* @param args
*/
public static void main(String args[]) throws IllegalArgumentException, NullPointerException
{
try
{
LinkedHashMap<Integer, Integer> lhm=new LinkedHashMap<Integer, Integer>(); // creating a new LinkedHashMap
lhm.put(1, new Integer(7)); // adding elements to this LinkedHashMap
lhm.put(2, new Integer(5));
lhm.put(3, new Integer(8));
lhm.put(4, new Integer(2));
lhm.put(5, new Integer(9));
lhm.put(6, new Integer(52));
System.out.println("The contents of the LinkedHashMap are as follows: ");
Set set=lhm.entrySet(); // creating a set of the contents of the LinkedHashMap
Iterator itr=set.iterator(); // invoking the iterator on the set created
while(itr.hasNext())
{
Map.Entry<Integer, Integer> map=(Map.Entry<Integer, Integer>)itr.next(); // using the Map.Entry interface
Integer key = map.getKey(); // getting the keys
Integer value = map.getValue(); // getting the values
System.out.print("KEY: "+key); // printing the keys
System.out.print(" "+"VALUE: "+value); // printing the values
System.out.println();
}

LinkedHashMap<Integer, Integer> cloneMap=new LinkedHashMap<Integer, Integer>();
cloneMap=(LinkedHashMap<Integer, Integer>)lhm.clone();
System.out.println("The contents of the cloned LinkedHashMap are as follows: ");
set=lhm.entrySet(); // creating a set of the contents of the cloned LinkedHashMap
itr=set.iterator(); // invoking the iterator on the set created
while(itr.hasNext())
{
Map.Entry<Integer, Integer> map=(Map.Entry<Integer, Integer>)itr.next(); // using the Map.Entry interface
Integer key = map.getKey(); // getting the keys of the cloned LinkedHashMap
Integer value = map.getValue(); // getting the values of the cloned LinkedHashMap
System.out.print("KEY: "+key); // printing the keys
System.out.print(" "+"VALUE: "+value); // printing the values
System.out.println();
}
}

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

}


}
The output of the above given example is the following one :

The contents of the LinkedHashMap are as follows:
KEY: 1  VALUE: 7
KEY: 2  VALUE: 5
KEY: 3  VALUE: 8
KEY: 4  VALUE: 2
KEY: 5  VALUE: 9
KEY: 6  VALUE: 52
The contents of the cloned LinkedHashMap are as follows:
KEY: 1  VALUE: 7
KEY: 2  VALUE: 5
KEY: 3  VALUE: 8
KEY: 4  VALUE: 2
KEY: 5  VALUE: 9
KEY: 6  VALUE: 52

Previous Home Next