public static <K,V extends Comparable<V>>List<K> sortMapAndLimitToList(Map<K,V> map,int k,boolean keepHighest){
  List<Entry<K,V>> sorted=sortByValue(map);
  if (keepHighest)   Collections.reverse(sorted);
  List<K> res=new ArrayList<K>();
  int count=0;
  for (  Map.Entry<K,V> e : sorted) {
    if (count >= k)     break;
 else     res.add(e.getKey());
    count++;
  }
  return res;
}

public static < K , V extends Comparable < V > > List < K > sortMapAndLimitToList ( Map < K , V > map , int k , boolean keepHighest ) { List < Entry < K , V > > sorted = sortByValue ( map ) ; if ( keepHighest ) Collections . reverse ( sorted ) ; List < K > res = new ArrayList < K > ( ) ; int count = 0 ; for ( Map . Entry < K , V > e : sorted ) { if ( count >= k ) break ; else res . add ( e . getKey ( ) ) ; count ++ ; } return res ; }
generic method to sort a map by value and then return the top k keys
