title: Most frequent n - words in java ArrayList
body: <p>I need to find the most frequent words (n words, so if n = 5, the most frequent 5 words) in an ArrayList.</p>
  
  <pre><code>private ArrayList&lt;String&gt; wordList = new ArrayList&lt;String&gt;();
  
  
  public ArrayList&lt;String&gt; mostOften(int k)
  {
      ArrayList&lt;String&gt; lista = new ArrayList&lt;String&gt;();
      Set&lt;String&gt; unique = new HashSet&lt;String&gt;(wordList);
      for (String key : unique) 
          System.out.println(key + ": " + Collections.frequency(wordList, key));
  
      return lista;
  }
  </code></pre>
  
  <p>The function needs to return a list of the most frequent words, sorted by frequency. If 2 words have the same frequency, I need to sort them alphabetaclly. I have posted what I tried, but this only finds the frequency and I do not know how to do the rest. Any help?</p>