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

how to create a clone of an EnumMap using collections in java

Previous Home Next

 

In this example we will see how to create the clone of an EnumMap.

For using an EnumMap class first of all we have to import the java.util package in which the java.util.EnumMap is defined. In this example we have created a class named CollectionExample in which we have a created an EnumMap object and added elements to it.
 In this example for creating the clone of an EnumMap we have used the clone() method on the object of the EnumMap. This clone() method have the general syntax as,
EnumMap<Object key, Obejct value> clone()
This method returns a shallow copy of the current EnumMap.

We have also created a user defined method named Createclone(EnumMap obj, Object obj) which takes the EnumMap object and Object object as its parameters. This method will create a clone of the given EnumMap obj to the Object obj and will display its contents.

In this example we have created an enumeration named cards and defined several values in it. Then we have created an EnumMap whose keys extends the enumeration cards and whose values are of String types. We have created an object of the EnumMap cardMap by using the default constructor of the EnumMap and displayed its contents.
We have also created an object of the Object class i.e obj and then called the Createclone(cardMap, obj) method. In this method we have stored the values returned by applying the clone() method on the emap. We have also created another EnumMap object named empclone then put all the values of the Object obj by using the typecasting.
Finally we have displayed all the values of the empclone by using the Iterator interface and Map.Entry interface.

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


public class CollectionExample
{

/**
* @param args
*/

public static void main(String args[]) throws NullPointerException
{
try
{
EnumMap<cards, String> cardMap=new EnumMap<cards, String>(cards.class); // this statement creates an object of EnumMap
cardMap.put(cards.hearts, "King of hearts"); // It puts the values on the basis of key-value pair
cardMap.put(cards.daimond, "Queen of diamonds");
cardMap.put(cards.spade, "Ace of spades");
cardMap.put(cards.clubs, "Jack of clubs");
for(cards card:cards.values()) // iterate over the cards
{
System.out.println(card+"----->"+cardMap.get(card)); // printing the key-value pair
}

Set<Map.Entry<cards,String>> set=cardMap.entrySet(); // creating a set of the entries of the EnumMap

Iterator<Map.Entry<cards, String>> itr=set.iterator(); // Invoking the iterator on the set
while(itr.hasNext())
{
Map.Entry<cards, String> mapobject=(Map.Entry<cards, String>)itr.next(); // using the Map.Entry interface
cards cardkey=mapobject.getKey(); // getting the keys
String value=mapobject.getValue(); // getting the values
System.out.print("cardKey=: "+cardkey); // displaying the keys
System.out.print(" "+"Value=: "+value); // displaying the values
System.out.println(); // printing a new line
}


Object obj=new Object(); // created an obect of Object class
Createclone(cardMap, obj); // calling the Createclone method

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




}




/* Following method creates a clone of an EnumMap */
/* This method takes two parameters viz. EnumMap object and Object's object */
private static void Createclone(EnumMap<cards, String> emap, Object obj)
{
obj=emap.clone(); // storing the clone of EnumMap object emap to Object's obj
EnumMap<cards, String> emapclone; // created a reference object of EnumMap
emapclone=(EnumMap<cards, String>)obj; // stored the Object's obj to EnumMap via typecasting
Set<Map.Entry<cards, String>> set=emapclone.entrySet(); // created a set of cloned EnumMap
Iterator<Map.Entry<cards, String>> itr=set.iterator(); // Invoking the iterator on the set thus created
System.out.println("The cloned EnumMap has following elements: ");
while(itr.hasNext())
{
Map.Entry<cards, String> mapclone=(Map.Entry<cards, String>)itr.next(); // using the Map.Entry Interface
cards key = mapclone.getKey(); // getting the keys
String value = mapclone.getValue(); // getting the values
System.out.print("Key: "+key); // displaying the keys
System.out.print("Value: "+value); // displaying the values
System.out.println();
}

}
}


enum cards // creating an enumeration of cards
{
hearts, daimond, spade, clubs;
}








The output of the above example is given below as:

hearts----->King of hearts
daimond----->Queen of diamonds
spade----->Ace of spades
clubs----->Jack of clubs
cardKey=: hearts  Value=: King of hearts
cardKey=: daimond  Value=: Queen of diamonds
cardKey=: spade  Value=: Ace of spades
cardKey=: clubs  Value=: Jack of clubs
The cloned EnumMap has following elements:
Key: heartsValue: King of hearts
Key: daimondValue: Queen of diamonds
Key: spadeValue: Ace of spades
Key: clubsValue: Jack of clubs

Previous Home Next