how to use an iterator over an EnumMap using collections in java
Previous | Home | Next |
In this example we will access the elements and keys of an EnumMap via using Iterator interface.
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 we have used the following methods,
put(Object key, Object value)
This method put the specified value with respect to the specified key.
Set<Map.Entry<Object key, Object value>> entrySet()
This method returns a set of view of the mappings contained in the current EnumMap.
In the given example, we have created a class named CollectionExample in which we created an EnumMap whose keys extends the enumeration cards and whose values are of String types.
We have also created a Set of the values of the EnumMap by invoking the entrySet() method on the object of EnumMap i.e. cardMap(in our example). Then, by using the Iterator interface we have invoked the iterator() method on the object of Set i.e set.
By using the while condition till the iterator reference itr has more elements to traverse we have displayed the keys and values of the given EnumMap by using the 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
System.out.println(set); // displaying the values of the set thus created
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
}
}
catch(NullPointerException e)
{
e.printStackTrace();
}
}
}
enum cards // creating an enumeration of cards
{
hearts, daimond, spade, clubs;
}
The output of the above example is given as:
hearts----->King of hearts
daimond----->Queen of diamonds
spade----->Ace of spades
clubs----->Jack of clubs
[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
Previous | Home | Next |