public static < K extends Comparable < ? super K > , V extends Comparable < ? super V > > List < Entry < K , V > > sortKeyValuePairByValue ( Map < K , V > map ) { List < Map . Entry < K , V > > entries = new ArrayList < Map . Entry < K , V > > ( map . entrySet ( ) ) ; Collections . sort ( entries , new Comparator < Map . Entry < K , V > > ( ) { @ Override public int compare ( Entry < K , V > o1 , Entry < K , V > o2 ) { if ( ! o1 . getValue ( ) . equals ( o2 . getValue ( ) ) ) { return ( o1 . getValue ( ) ) . compareTo ( o2 . getValue ( ) ) ; } return o1 . getKey ( ) . compareTo ( o2 . getKey ( ) ) ; } } ) ; return entries ; }
Utility method to sort the keys and values of a map by their value .