private void trim(boolean finalTrim) throws HiveException {
    ArrayList<Map.Entry<ArrayList<String>,Double>> list = new ArrayList(ngrams.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<ArrayList<String>,Double>>() {
      @Override
      public int compare(Map.Entry<ArrayList<String>,Double> o1,
                         Map.Entry<ArrayList<String>,Double> o2) {
        return o1.getValue().compareTo(o2.getValue());
      }
    });
    for(int i = 0; i < list.size() - (finalTrim ? k : pf*k); i++) {
      ngrams.remove( list.get(i).getKey() );
    }
  }
Trims an n-gram estimation down to either 'pf' * 'k' n-grams, or 'k' n-grams if
finalTrim is true.
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/NGramEstimator.java