Get Set view of Keys from TreeMap - Java Collection Framework

Java examples for Collection Framework:TreeMap

Description

Get Set view of Keys from TreeMap

Demo Code


import java.util.Iterator;
import java.util.TreeMap;
import java.util.Set;
 
public class Main {
 
  public static void main(String[] args) {
   /* w w  w .  j av  a2  s  .co  m*/
    //create TreeMap object
    TreeMap treeMap = new TreeMap();
   
    //add key value pairs to TreeMap
    treeMap.put("1","One");
    treeMap.put("3","Three");
    treeMap.put("2","Two");
   
    Set st = treeMap.keySet();
   
    System.out.println("Set created from TreeMap Keys contains :");
   
    Iterator itr = st.iterator();
    while(itr.hasNext())
      System.out.println(itr.next());
     
    //remove 1 from Set
    st.remove("1");
   
    //check if original TreeMap still contains 2
    boolean blnExists = treeMap.containsKey("1");
    System.out.println("Does TreeMap contain 1 ? " + blnExists);
  }
}

Result


Related Tutorials