I want to find the largest value in a Hashtable of Integer values. Is there any quick and efficient way to achieve this?
This is my code…
Hashtable<String,Integer> h = new Hashtable<String,Integer>();
h.add( "a",1 );
h.add( "b",5 );
h.add( "c",3 );
h.add( "d",5 );
h.add( "e",2 );
h.add( "f",1 );
int max = ???;
I need to find the maximum value, which in the example above is 5. The Hashtable will always be small, less than 100 entries on average.
Use
Collections#max()onMap#values().Note that you should be using
Map#put()to put the elements, there’s noMap#add().