title: How to store n-highest values and how to ignore words less than a min length?
body: <p>Assume we have the following function which finds the highest frequent word of a passage:</p>
  
  <pre><code>private static String countKeyword(File from) throws IOException {
      final Path path = Paths.get(from.getPath());
      final Map&lt;String, Integer&gt; counts = countOccurrences(path);
  
      Map.Entry&lt;String, Integer&gt; maxEntry = null;
  
      for (Map.Entry&lt;String, Integer&gt; entry : counts.entrySet())
      {
          if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) &gt; 0)
          {
              maxEntry = entry;
          }
      }
      System.out.println(maxEntry.getKey());
      return maxEntry.getKey();   
  }
  
  private static Map&lt;String, Integer&gt; countOccurrences(Path path) throws IOException {
      final Pattern pattern = Pattern.compile("[gG][a-bA-B].+",Pattern.CASE_INSENSITIVE);
  
      try (final Stream&lt;String&gt; lines = Files.lines(path))
      {
          return lines
              .flatMap(pattern::splitAsStream)
              .collect(toMap(identity(), w -&gt; 1, Integer::sum));
      }
  }
  </code></pre>
  
  <p>Now how can we change this to another function which returns the n-highest frequent words? And how can we change the <code>countOccurrences(Path path)</code> so it ignores words less than a min length?</p>