title: Java - Compare frequency of elements of a specific type
body: <p>my task is to read a file and then print out the most 10 frequent words in the text file.</p>
  
  <p>I have used the following comparator and it gives me correctly the first 3 most frequent words but then it kind of gets it all wrong and it only prints out 9 instead of 10.</p>
  
  <p>Here is my comparator</p>
  
  <pre><code>import java.util.*;
  
  class CompareFreq implements Comparator&lt;HashElement&gt;
  {
      public int compare(HashElement x, HashElement y)
      {
          if (y.getCounter() &gt; x.getCounter())
          {
              return 1;
          }
  
          else
          {
              return -1;
          }
      }
  }
  </code></pre>
  
  <p>And here is my main:</p>
  
  <pre><code>        String[] temp = new String[100000];
          Scanner obj = new Scanner(new File("file.txt"));
  
          while (obj.hasNext())
          {
              String text = obj.nextLine();
  
              for (String t : text.split(" "))
              {
                  set.insert(t);
              }
          }
  
          System.out.println("All words and the frequency:");
          set.printTableInOrderAndFreq();
  
          System.out.println();
          System.out.println("Most 10 frequent words:");
          set.getMaxWordsFrequency(10);
  </code></pre>
  
  <p>Here is the output that I am getting </p>
  
  <pre><code>Most 10 frequent words:
  to
  the
  of
  their
  they
  
  caused
  with
  companions
  great
  </code></pre>
  
  <p>The first 3 (to, the, of) words are correct, they are the most frequent in my text but then the calculations are wrong. On top of that I believe it is deleting on of the 10 words where you can see there's a blank row between the word 'they' and 'caused'</p>
  
  <p>I forgot to post my HashElement class, here it is</p>
  
  <pre><code>public class HashElement
  {
      private String word; // ordet
      public int counter; // antalet förekomst av ordet
      private boolean isActive; // behövs eventuellt för remove
  
      public HashElement(String theword)
      {
          word = theword;
          isActive = true;
      }
  
      public void increment()
      {
          counter++;
      }
  
      public int getCounter()
      {
          counter++;
          return counter;
      }
  
      public String getWord()
      {
          return word;
      }
  
      public boolean getStatus()
      {
          return isActive;
      }
  
      public boolean changeStatus()
      {
          isActive = false;
          return isActive;
      }
  }
  </code></pre>
  
  <p>Thanks for the help in advance</p>