private void processNgrams(NGramAggBuf agg, ArrayList<String> seq) throws HiveException {
      for(int i = seq.size()-agg.n; i >= 0; i--) {
        ArrayList<String> ngram = new ArrayList<String>();
        for(int j = 0; j < agg.n; j++)  {
          ngram.add(seq.get(i+j));
        }
        agg.nge.add(ngram);
      }
    }
A constant-space heuristic to estimate the top-k n-grams.
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDAFnGrams.java