code: public void keepTopNElements(int N){
          PriorityQueue<Pair<T, Double>> queue = asPriorityQueue();
          clear();
          for (int e = 0; e < N; e++) {
              Pair<T, Double> pair = queue.poll();
              if (pair != null)
                  incrementCount(pair.getFirst(), pair.getSecond());
          }
      }
docs: This method removes all elements except of top N by counter values
  @param N