how to add some other collection to an EnumMap using collections in java
Previous | Home | Next |
In this example we will add some collection which extends the Map to the given 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.
For adding all the contents of some collection that extends the Map we have used the putAll(Map map) which is having the following syntax,
void putAll(Map map)
The above method copies all of the mappings from the specified map to the current EnumMap.
Also we have created a user defined method named AddCollection(EnumMap obj, HashMap obj) which takes the EnumMap object and HashMap object as parameter.
In the given example we have created an enumertion named cards in which we have defined some elements. We have also created an EnumMap named cardMap in whose keys extends the enumeration cards elements and values are of String type. Then we have added some elements to the cardMap by using the put(Object key, Object value) on it and displayed its contents.
We have created an object of HashMap hmap by using the default constructor of the HashMap as,
HashMap<cards, String> hmap=new HashMap<cards, String>();
Then we have called the AddCollection(cardMap, hmap) method which adds all the contents of the hmap to the cardMap.
Inside the AddCollection(cardMap, hmap) method we have added the elements to the HashMap by using the put(Object key, Obejct value) method on its object i.e hmap. Then we have used the addAll(Map map) on the object of the EnumMap i.e emap and then displayed all the values of the EnumMap by using the Iterator 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
}
HashMap<cards, String> hmap=new HashMap<cards, String>();
AddCollection(cardMap, hmap);
}
catch(NullPointerException e)
{
e.printStackTrace();
}
}
/* Following method adds some collection to the EnumMap */
/* This method takes two arguments one is EnumMap object and other is HashMap object */
private static void AddCollection(EnumMap<cards, String> emap, HashMap<cards, String> hmap)
{
hmap.put(cards.clubs, "r4r"); // adding elements to the HashMap
hmap.put(cards.daimond, "tutorials");
hmap.put(cards.spade, "development");
hmap.put(cards.hearts, "softwares");
System.out.println("The contents of the HashMap created are: "+hmap); // displaying the contents of the HashMap
emap.putAll(hmap); // putting all the contents of the HashMap to the EnumMap
System.out.println("After adding the contents of the hashmap to EnumMap its contents are: ");
Set<Map.Entry<cards, String>> set=emap.entrySet(); // creating a set of the elements of the EnumMap
Iterator<Map.Entry<cards, String>> itr=set.iterator(); // Invoking the iterator over the set created
while(itr.hasNext())
{
Map.Entry<cards, String> map=(Map.Entry<cards, String>)itr.next(); // using Map.Entry interface
cards key=map.getKey(); // getting the keys
String value=map.getValue(); // getting the values
System.out.print("Keys: "+key); // printing the keys
System.out.print(" "+"Values: "+value); // printing the values
System.out.println(); // printing the newline
}
}
}
enum cards // creating an enumeration of cards
{
hearts, daimond, spade, clubs;
}
The output of the above given example is shown 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 contents of the HashMap created are: {daimond=tutorials, clubs=r4r, hearts=softwares, spade=development}
After adding the contents of the hashmap to EnumMap its contents are:
Keys: hearts Values: softwares
Keys: daimond Values: tutorials
Keys: spade Values: development
Keys: clubs Values: r4r
Previous | Home | Next |