I want to find all "entry pairs with maximum value" from a Hashtable, my Hashtable is like this --
Hashtable<Integer, Integer> ht = new Hashtable<Integer, Integer>();
ht.put(1, 4);
ht.put(2, 2);
ht.put(3, 4);
ht.put(4, 2);
ht.put(5, 4);
I want to find these key-value pairs: <1,4>, <3,4>, <5,4>
, I understand it could be done by finding the maximum valued entry first, then reiterate through the Hashtable to find other similar entries. But I was wondering if there is any elegant/simpler way to do this.
any idea ?
int max = Integer.MIN_VALUE;
final List< Entry< Integer, Integer > > maxList =
new ArrayList< Entry< Integer, Integer > >();
for ( final Entry< Integer, Integer > entry : ht.entrySet() ) {
if ( max < entry.getValue() ) {
max = entry.getValue();
maxList.clear();
}
if ( max == entry.getValue() )
maxList.add( entry );
}