Key for maximum value in Hashtable
hashtable, java, max
Solution
There is no built in function to get the maximum value out of a `Hashtable` you are going to have to loop over all the keys and manually determine the max.
Object maxKey=null;
Double maxValue = Double.MIN_VALUE;
for(Map.Entry<Object,Double> entry : table.entrySet()) {
if(entry.getValue() > maxValue) {
maxValue = entry.getValue();
maxKey = entry.getKey();
}
}
Edit: To find more than 1 key for the max value
ArrayList<Object> maxKeys= new ArrayList<Object>();
Double maxValue = Double.MIN_VALUE;
for(Map.Entry<Object,Double> entry : table.entrySet()) {
if(entry.getValue() > maxValue) {
maxKeys.clear(); /* New max remove all current keys */
maxKeys.add(entry.getKey());
maxValue = entry.getValue();
}
else if(entry.getValue() == maxValue)
{
maxKeys.add(entry.getKey());
}
}
Problem
Hi I have the following object: ``` Hashtable<Object, Double> ``` and I want to find the key of the maximum Double value in the table. Easiest way to do that? Thanks