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 ( ) ) ; } }
This method removes all elements except of top N by counter values