Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Wednesday, 18 June 2014

3 ways of loop or iterate through a Map implementation HashMap or TreeMap in Java

In recent days Collections is becoming a very common area of core java to be asked in interviews. When it comes to Collections in Java Map interface and its implementations like HashMap or TreeMap are the most concentrating areas to be picked by the interviewer. In this particular blog we will see how to loop a Map or how to iterate over a Map in Java. By the end of this tutorial you will be able to answer below mentioned questions.

1) How to loop or iterate over a map in Java
2) How to loop or iterate over a HashMap in Java
3) How to loop or iterate over a TreeMap in Java


How to loop or iterate through a Map in Java


There are 4 most common ways of loop or iterating through a Map implementation in Java, here I am explaining them all one by one:

1) Loop or iterating through a HashMap in Java using Iterator interface.


To iterate through a HashMap or loop a HashMap using Iterator we have to get an Iterator over EntrySet of the targeted HashMap. Than we can iterate or loop the EntrySet to get map values and keys from it, see the example below.
package com.beingjavaguys.collectiondemo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class MapDemo {

 /**
  * @Author Nagesh Chauhan
  */
 public static void main(String[] args) {
  Map<Integer, String> hashMap = new HashMap<Integer, String>();
  hashMap.put(1, "Sunday");
  hashMap.put(2, "Monday");
  hashMap.put(3, "Tuesday");
  hashMap.put(4, "Wednesday");
  hashMap.put(5, "Thursday");
  hashMap.put(6, "Friday");
  hashMap.put(6, "Sataurday");

  Iterator<Entry<Integer, String>> mapIterator = hashMap.entrySet()
    .iterator();

  while (mapIterator.hasNext()) {
   Map.Entry<Integer, String> mapEntry = mapIterator.next();
   System.out.println("The key is: " + mapEntry.getKey()
     + ", value is :" + mapEntry.getValue());
  }
 }

}


2) Loop or iterating through a HashMap in Java using for loop


Here we have used foreach loop added from JDK5 for iterating over any map in java and using KeySet of map for getting keys. this will iterate through all values of Map and display key and value together.
package com.beingjavaguys.collectiondemo;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class MapDemo {

 /**
  * @Author Nagesh Chauhan
  */
 public static void main(String[] args) {
  Map<Integer, String> hashMap = new HashMap<Integer, String>();
  hashMap.put(1, "Sunday");
  hashMap.put(2, "Monday");
  hashMap.put(3, "Tuesday");
  hashMap.put(4, "Wednesday");
  hashMap.put(5, "Thursday");
  hashMap.put(6, "Friday");
  hashMap.put(6, "Sataurday");

  for (Entry<Integer, String> entry : hashMap.entrySet()) {
   System.out.println("Key : " + entry.getKey() + " Value : "
     + entry.getValue());
  }
 }

}


3) Loop or iterating through a HashMap in Java using key as an Object


This is third and last way of iterating over a hashmap or any other implementation of map interface, we have used key of an Map as Object and used for each type of loop to print key and values from a hashmap.
package com.beingjavaguys.collectiondemo;

import java.util.HashMap;
import java.util.Map;

public class MapDemo {

 /**
  * @Author Nagesh Chauhan
  */
 public static void main(String[] args) {
  Map<Integer, String> hashMap = new HashMap<Integer, String>();
  hashMap.put(1, "Sunday");
  hashMap.put(2, "Monday");
  hashMap.put(3, "Tuesday");
  hashMap.put(4, "Wednesday");
  hashMap.put(5, "Thursday");
  hashMap.put(6, "Friday");
  hashMap.put(6, "Sataurday");

  for (Object key : hashMap.keySet()) {
   System.out.println("Key : " + key + " Value : " + hashMap.get(key));
  }
 }

}


In this particular blog we came across '3 ways of loop or iterate through a Map implementation HashMap or TreeMap in Java' in upcoming blogs we will see more about 'Maven', 'Spring', 'Java' and other open source technologies.








Thanks for reading !
Being Java Guys Team




0 comments:

Post a Comment

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft