Priority queue of pairs { frequency , value }
PriorityQueue < pair > pq = new PriorityQueue < > ( ( a , b ) -> a . first - b . first ) ; for ( Map . Entry < Integer , Integer > k : mp . entrySet ( ) ) pq . add ( new pair ( k . getValue ( ) , k . getKey ( ) ) ) ;