Iterate through the values of TreeMap - Java Collection Framework

Java examples for Collection Framework:TreeMap

Description

Iterate through the values of TreeMap

Demo Code

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

Result


Related Tutorials