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 the Map.Entry interface on the HashMap using collections in java

how to use the Map.Entry interface on the HashMap using collections in java

Previous Home Next

 

In this example we shall see how can we use the Map.Entry interface on the HashMap.

For creating a HashMap first of all we have to import the java.util package in which the java.util.HashMap class is contained which is used for creating the HashMap. In this example we have created a class named CollectionExample in which we have created HashMap and added some elements to it.
 In this example we have used the Map.Entry interface, and then we have used separately accessed the keys and values of the HashMap by using getKey() and getValue() method on the map object.

In this example we have created a HashMap and then added some elements to it. We have also created a Set of the elements of the HashMap using the entrySet() method on the HashMap object i.e hm. Then we have used the iterator() method of the Iterator interface on the set. We have created an reference of the Map.Entry interface and typecasted the values returned by the iterator to Map.Entry. Then finally we have displayed the values contained in the Map.Entry reference object by using the getKey() and getValue() methods.

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


public class CollectionExample {

/**
* @param args
*/
public static void main(String args[]) throws IllegalArgumentException, NullPointerException
{
try
{
HashMap<String, Integer> hm = new HashMap<String, Integer>(); // creating a HashMap hm
hm.put("one-> ", 1); // adding value to the HashMap on key value pair
hm.put("two-> ", 2);
hm.put("three-> ", 3);
hm.put("four-> ", 4);
hm.put("five-> ", 5);
System.out.println("The key contained in the HashMap are as follows: ");

Set set=hm.entrySet(); // creating a set of the key-value pairs contained in the HashMap
Iterator itr=set.iterator(); // Invoking iterator on the HashMap
System.out.println("The key value pair in contained in the hashmap are as follows: ");

while(itr.hasNext())
{
Map.Entry<String, Integer> m = (Map.Entry<String, Integer>)itr.next(); // Using Map.Entry Interface
String key=(String)m.getKey(); // retrieving the keys contained the map entry interafce object
Integer value=(Integer)m.getValue(); // retrieving the keys contained in the map entru interface object

System.out.print(key); // displaying the keys contained in the HashMap
System.out.println(value); // displaying the values contained in the HashMap
}
}

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


}


The output of the above given example is as follows: 

The key contained in the HashMap are as follows:
The key value pair in contained in the hashmap are as follows:
two-> 2
one-> 1
five-> 5
three-> 3
four-> 4

Previous Home Next