title: Infinite Loop Iterating Through Hashtable
body: <p>I'm trying find the most popular word in an array using Hashtables. For some reason the while loop is looping infinitely. I've debugged and the element never changes from the first one it gets. Any ideas on why this is happening?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.util.Hashtable;
  
  public class MyClass {
    public String mostPopularString (String []words) {
      if (words == null)
              return null;
      if (words.length == 0)
              return null;
      Hashtable&lt;String, Integer&gt; wordsHash = new Hashtable&lt;String, Integer&gt;();
      for (String thisWord : words)
      {
          if (wordsHash.containsKey(thisWord))
          {
              wordsHash.put(thisWord, wordsHash.get(thisWord) + 1);
          }
          else
          {
              wordsHash.put(thisWord, 1);
          }
      }
      Integer mostPopularCount = 0;
      String mostPopularWord = null;
      boolean tie = false;
      while (wordsHash.keys().hasMoreElements())
      {
          String currentWord = (String) wordsHash.keys().nextElement();
          if (wordsHash.get(currentWord) &gt; mostPopularCount)
          {
              mostPopularCount = wordsHash.get(currentWord);
              mostPopularWord = currentWord;
              tie = false;
          }
          else if (wordsHash.get(currentWord) == mostPopularCount)
          {
              tie = true;
          }
      }
      if (tie)
          return null;
      else
          return mostPopularWord;
    }
  }
  </code></pre>