Powered by Blogger.

How to Iterate Map using Generics

>> Thursday, June 9, 2011

Map is an interface.We can iterate the implemented classes from map like TreeMap,HashMap,LinkedHashMap.

Maps do not provide an iterator() method as do Lists and Sets. A Set of either keys (keySet()) or key-value 'Map.Entry' elements (entrySet()) can be obtained from the Map, and one can iterate over that.

Create an object for HashMap and insert the objects in that.

Map<Integer, String> map = new HashMap<Integer, String>();
  map.put(100, "Murali");
  map.put(200, "khaleel");
  map.put(300, "Bhaskar");


With that object if you want to retrieve only keys from HashMap then we should convert it into Set and while iteration by using getKey() method.

Set<integer> set = map.keySet();
  Iterator<integer> it = set.iterator();
  while(it.hasNext()){
   System.out.println(it.next());
  }

To retreive the values from the HashMap we can't use the Set why because by using map.values() method returns Collection of objects, so we should convert it into Collection. So take the Collection object in String object.


Collection<string> col = map.values();
  Iterator<string> it1 = col.iterator();
  while(it1.hasNext()){
   System.out.println(it1.next());
  }

Here we are using Generics to mention their type of keys and values in HashMap.Here we are iterating keys and values in HashMap.For that better to take map.entrySet() to iterate the Map keys and values.

In that we can retrieve the keys in Iteration block with getKey() and values as getValue().

Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();
  while(entries.hasNext()){
    Map.Entry<Integer, String> entry = entries.next();
    System.out.print(entry.getKey()+" , ");
    System.out.print(entry.getValue()+"n");
  }


The Entire program for Iteration of Map:

package javabynataraj.collections;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapToSet{
 public static void main(String[] args) {
  
  Map<Integer, String> map = new HashMap<Integer, String>();
  map.put(100, "Murali");
  map.put(200, "khaleel");
  map.put(300, "Bhaskar");
  
  System.out.println("The HashMap with keys and values ");
  System.out.println(map);
  
  System.out.println("Retrieving keys from HashMap");
  Set<Integer> set = map.keySet();
  Iterator<Integer> it = set.iterator();
  while(it.hasNext()){
   System.out.println(it.next());
  }
  
  System.out.println("Retrieving values from HashMap");
  Collection<String> col = map.values();
  Iterator<String> it1 = col.iterator();
  while(it1.hasNext()){
   System.out.println(it1.next());
  }
  
  System.out.println("Retrieving keys and values from HashMap");
   
  Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();
  while(entries.hasNext()){
    Map.Entry<Integer, String> entry = entries.next();
    System.out.print(entry.getKey()+" , ");
    System.out.print(entry.getValue()+"n");
  }
 }
}

The OutPut is:
Reference Books:

Head First Java, 2nd Edition  Java (A Beginner's Tutorial) The Java Tutorial: A Short Course on the Basics, 4th Edition

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2014. All rights reserved.