title: get the three highest values in a TreeMap
body: <p>I am trying to find the three highest values in a TreeMap. I wrote a code that is kind of doing it, but I would like to ask whether you can suggest a more efficient way.
  Basically, I am saving each word of my text in a TreeMap along with the number of times it appears in the text. Then I am using a comparator to sort the values. Then I am iterating through the newly created Map until I reach the last three values, which are the highest values after the sorting and print them out. I am going to use large texts, so this is not a very good way. 
  Here is my code:</p>
  
  <pre><code>class Text{
      public static void main(String args[]) throws FileNotFoundException, IOException{
          final File textFile = new File("C://FileIO//cinderella.txt"); 
          final BufferedReader in = new BufferedReader(new FileReader(textFile));                               
          final TreeMap&lt;String, Integer&gt; frequencyMap = new TreeMap&lt;String, Integer&gt;(); 
  
          String currentLine; 
          while ((currentLine = in.readLine()) != null) {  
              currentLine = currentLine.toLowerCase();  
              final StringTokenizer parser = new StringTokenizer(currentLine, " \t\n\r\f.,;:!?'"); 
              while (parser.hasMoreTokens()) { 
                  final String currentWord = parser.nextToken(); 
                  Integer frequency = frequencyMap.get(currentWord); 
                  if (frequency == null) { 
                      frequency = 0; 
                  } 
                  frequencyMap.put(currentWord, frequency + 1);
              } 
          }  
  
          System.out.println("This the unsorted Map: "+frequencyMap);
  
          Map sortedMap = sortByComparator(frequencyMap);
          int i = 0;
          int max=sortedMap.size();
          StringBuilder query= new StringBuilder();
  
          for (Iterator it = sortedMap.entrySet().iterator(); it.hasNext();) {
              Map.Entry&lt;String,Integer&gt; entry = (Map.Entry&lt;String,Integer&gt;) it.next();
              i++;
              if(i&lt;=max &amp;&amp; i&gt;=(max-2)){
                  String key = entry.getKey();
                  //System.out.println(key);
                  query.append(key);
                  query.append("+");
              }
          }
          System.out.println(query);
      }
  
      private static Map sortByComparator(TreeMap unsortMap) {
          List list = new LinkedList(unsortMap.entrySet());
  
          //sort list based on comparator
          Collections.sort(list, new Comparator() {
              public int compare(Object o1, Object o2) {
                  return ((Comparable) ((Map.Entry) (o1)).getValue())
                         .compareTo(((Map.Entry) (o2)).getValue());
              }
          });
  
          //put sorted list into map again
          Map sortedMap = new LinkedHashMap();
          for (Iterator it = list.iterator(); it.hasNext();) {
              Map.Entry entry = (Map.Entry)it.next();
              sortedMap.put(entry.getKey(), entry.getValue());
  
          }
          return  sortedMap;
      }   
  }
  </code></pre>