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 |