title: Java: Getting the 500 most common words in a text via HashMap
body: <p>I'm storing my wordcount into the value field of a HashMap, how can I then get the 500 top words in the text?</p>
  
  <pre><code> public ArrayList&lt;String&gt; topWords (int numberOfWordsToFind, ArrayList&lt;String&gt; theText) {
  
          //ArrayList&lt;String&gt; frequentWords = new ArrayList&lt;String&gt;();
  
          ArrayList&lt;String&gt; topWordsArray= new ArrayList&lt;String&gt;();
  
          HashMap&lt;String,Integer&gt; frequentWords = new HashMap&lt;String,Integer&gt;();
  
          int wordCounter=0;
  
          for (int i=0; i&lt;theText.size();i++){
  
  
  
                    if(frequentWords.containsKey(theText.get(i))){
  
                         //find value and increment
                        wordCounter=frequentWords.get(theText.get(i));
                        wordCounter++;
                        frequentWords.put(theText.get(i),wordCounter);
  
                    }
  
                  else {
                    //new word
                    frequentWords.put(theText.get(i),1);
  
                  }
          }
  
  
          for (int i=0; i&lt;theText.size();i++){
  
              if (frequentWords.containsKey(theText.get(i))){
                   // what to write here?
                  frequentWords.get(theText.get(i));
  
              }
          }
          return topWordsArray;
      }
  </code></pre>