title: Iterator over HashMap in Java
body: <p>I tried to iterate over hashmap in Java, which should be a fairly easy thing to do. However, the following code gives me some problems:</p>
  
  <pre><code>HashMap hm = new HashMap();
  
  hm.put(0, "zero");
  hm.put(1, "one");
  
  Iterator iter = (Iterator) hm.keySet().iterator();
  
  while(iter.hasNext()) {
  
      Map.Entry entry = (Map.Entry) iter.next();
      System.out.println(entry.getKey() + " - " + entry.getValue());
  
  }
  </code></pre>
  
  <p>First, I needed to cast Iterator on hm.keySet().iterator(), because otherwise it said "Type mismatch: cannot convert from java.util.Iterator to Iterator". But then I get "The method hasNext() is undefined for the type Iterator", and "The method hasNext() is undefined for the type Iterator".</p>