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

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