Get Set view of Keys from HashMap - Java Collection Framework

Java examples for Collection Framework:HashMap

Description

Get Set view of Keys from HashMap

Demo Code


import java.util.Iterator;
import java.util.HashMap;
import java.util.Set;
 
public class Main {
 
  public static void main(String[] args) {
    HashMap hMap = new HashMap();
   /*from w  w  w. j  a v  a  2  s.  co  m*/
    hMap.put("1","One");
    hMap.put("2","Two");
    hMap.put("3","Three");
   
    Set st = hMap.keySet();
   
    System.out.println("Set created from HashMap Keys contains :");

    Iterator itr = st.iterator();
    while(itr.hasNext())
      System.out.println(itr.next());
     
    st.remove("2");
   
    boolean blnExists = hMap.containsKey("2");
    System.out.println("Does HashMap contain 2 ? " + blnExists);
  }
}

Result


Related Tutorials