Ads Sponsored By Google :)

Wednesday 14 August 2013

How many ways to Traverse HashMap in java With Example Code

how to traverse a hashmap in java,java code to traverse a hashmap,traverse a hashmap java ,ways of traverse the map in java, traversing hashmap using foreach loop, traversing hashmap using KeySet Iterator, traversing hashmap using entrySet, Java Code for

traversing Hashmap in java.


   In Java Collection Frame Work we have multiple ways of iterating the data from the Map, HashMap and TreeMap. As am java developer one of my colleagues attended the interview of experience of 4years on java. Then he got the question like this, write the java to iterate or retrieve the data from the hashmap or TreeMap or map in atleast four ways. So Candidate who going to attend for the java interview of experience of 2+, 3+, 4+,5+, 6+, 7+ etc..
You can find the this type of questions in the interview. Why because these candidates did so many projects in real time, so if the interviewer think that these guys must have command on retrieving the data through Collection Object  by using the HashMap. Map, TreeMap Objects.

In this tutorial we are going to retrieve the data from the Object of HashMap, Map, TreeMap. We are retrieving or traversing or iterating the data from HashMap, Map, TreeMap Object in five different ways.
HashMap Traversing Techinques

Consider an Example :

In real time most of the cases we are using generics with Collection classes and interface, so its better to write the code in Collections with Generics to impress the interviewer.

HashMap object creation along with Generics :

HashMap<String, String> demo=new HashMap<String,String>();
demo.put(”Michael”, “USA”);
demo.put(“Jaffar”,”Afghan”);

1) Iterating Map Object elements using the foreach loop :

From JDK 5.0 Version so many updating are included in it, in which one of the updation is foreach loop for iterating over map by using KeySet of Map interface to get the keys. This will used to iterate the values of Map and display the key and value in pair.

For Example:

HashMap<String, String> demo=new HashMap<String,String>();
demo.put(”Michael”, “USA”);
demo.put(“Jaffar”,”Afghan”);
for(String key : dmeo.keySet()){
System.out.println(key+”  ”+demo.get(key));
}

Output :

Iterating or Traversing the Map Using Java5 foreach loop
Michael USA
Jaffar Afghan

2) Iterating HashMap, Map, TreeMap in java using KeySet Iterator:

  To Retrieve the HashMap, Map, TreeMap by using Iterator 

For Example:
Set<String> keyset=demo.keySet();
Iterator<String> keysetIterator=keyset.iterator();
While(keysetIterator.hasNext()){
String  key=keysetIterator.hasNext();// stores Key “Michael”,”Jaffar”
System.out.println(key+”  “+demo.get(key));// USA, Afghan
}

Output :
Iterating or Traversing the Map Using Java5 Iterator
Michael USA
Jaffar Afghan

3) Retrieving or Traversing the HashMap in java using EntrySet along with forLoop :

From this Example we are retrieving the data using EntrySet along with foreachloop. EntrySet is collection of all Map Entries which contains Key and Value.

For Example :

Set<Map.Entry<String<String>> entryset-demo.entrySet();
For(Entry entry : entrySet()){
System.out.println(entry.getKey() +” “ +entry.getValue());
}

Output :

Iterating or Traversing the Map Using entrySet
Michael USA
Jaffar Afghan

4) Iterating HashMap in java using EntrySet and Iterator :

Retrieving the data from the HashMap object using EntrySet and Itertor.

For Example :

 Set<Map,Entry<String,String>> entrySet1=demo.entrySet();
 Itertor<Entyr<String, String>> entrySetIterator=entrySet1.iterator();
 While(entrySetIterator.hasNext()){
 Entry entry=entrySetIterator.next();
 System.out.println(entry.getKey()+”  ”+entry.getValue());
 }

Output:

Iterating or Traversing the Map Using Java5 Iterator & EntrySet
Michael USA
Jaffar Afghan

No comments:

Post a Comment

LinkWithin