title: Iterating over a HashMap, two ways: iterating with entry set & iterating with enumeration
body: <p>Are HashMaps supposed to be iterated in these two ways or is enumeration considered wrong? I made a simple iterator program with two iterator functions:</p>
  
  <pre><code>import java.util.*;
  
  public class HashMapIterators
  {
      public static void main(String[] args)
      {
          HashMap&lt;String, Integer&gt; hMap = new HashMap&lt;String, Integer&gt;();
  
          hMap.put("abc", 10);
          hMap.put("pqr", 20);
          hMap.put("asd", 30);
          hMap.put("xyz", 40);
  
          iteratorEnumeration(hMap);
          iteratorEntrySet(hMap);
      }
  
      public static void iteratorEnumeration(HashMap hMap)
      {
          System.out.println("iteratorEnumeration");
  
          Iterator it = hMap.keySet().iterator();
          Enumeration em = hMap.elements();               // doesn't work
          Enumeration em = new IteratorEnumeration(it);   // doesn't work
  
  //      while(em.hasMoreElements())
          {
          }
      }
  
      public static void iteratorEntrySet(HashMap hMap)
      {
          System.out.println("iteratorEntrySet");
  
          Iterator it = hMap.entrySet().iterator();
  
          while(it.hasNext())
          {
              Map.Entry me = (Map.Entry)it.next();
              System.out.println("Key: [" + me.getKey() + "], Value: [" + me.getValue() + "]");
          }
      }
  }
  </code></pre>
  
  <p>The iteratorEnumeration function does not work, I guess these methods (like elements()) is applicable on HashTable's only. Correct me if i'm wrong, I don't know when is it appropriate to use what function. So, my main question is - should we iterate over a HashMap by setting a iterator over the entrySet method (please see my iteratorEntrySet function), is this the only right way to do it?</p>