To store String which has maximum frequency
String s = " " ; for ( Map . Entry < String , Integer > i : mp . entrySet ( ) ) { if ( i . getValue ( ) > maxi ) { maxi = i . getValue ( ) ; s = i . getKey ( ) ; } else if ( i . getValue ( ) == maxi ) { String ss = i . getKey ( ) ; if ( ss . length ( ) > s . length ( ) ) s = ss ; } }