Iterate through the values of HashMap - Java Collection Framework

Java examples for Collection Framework:HashMap

Description

Iterate through the values of HashMap

Demo Code

 
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
 
public class Main {
 
  public static void main(String[] args) {
   /*from  ww  w. jav a 2  s .  c  o m*/
    HashMap hMap = new HashMap();
   
    //add key value pairs to HashMap
    hMap.put("1","One");
    hMap.put("2","Two");
    hMap.put("3","Three");
   
    Collection c = hMap.values();
   
    //obtain an Iterator for Collection
    Iterator itr = c.iterator();
   
    //iterate through HashMap values iterator
    while(itr.hasNext())
      System.out.println(itr.next());
  }
}

Result


Related Tutorials