Key for maximum value in Hashtable

Go To StackoverFlow.com

4

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

2012-04-04 04:43
by chopchop
You'd need to iterate through all the Keys - Brian Roach 2012-04-04 04:47


6

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());
     }
}
2012-04-04 04:56
by twain249
Can I know, what happens if their are two highest values? what I am suppose to do to find both values key? thx in advanc - Learner 2013-05-19 18:08
@Learner check the edit to handle multiple keys with the same max valu - twain249 2013-05-19 20:26


4

If it's really important you do it without iterating all keys, simply extend HashTable

class MyHashtable extends Hashtable<Object, Double> {

    private Double maxValue = Double.MIN_VALUE;

    @Override
    public synchronized Double put(Object k, Double v) {
        maxValue = Math.max(maxValue, v);
        return super.put(k, v);
    }

    @Override
    public synchronized void clear() {
        super.clear();
        maxValue = Double.MIN_VALUE;
    }

    public Double getMaxValue() {
        return maxValue;
    }

    @Override
    public synchronized Double remove(Object key) {
        // TODO: Left as an Excercise for the user, refer the other answers
        return super.remove(key);
    }
}
2012-04-04 05:06
by st0le


0

You can loop through and find the max value:

public static void main(String[] args) {
    Map<Object, Double> maps = new HashMap<Object, Double>();
    maps.put("5", new Double(50.0));
    maps.put("4", new Double(40.0));
    maps.put("2", new Double(20.0));
    maps.put("1", new Double(100.0));
    maps.put("3", new Double(30.0));
    maps.put("5", new Double(50.0));

    Double max = Double.MIN_VALUE;
    for(Object key: maps.keySet()) {
        Double tmp = maps.get(key);
        if(tmp.compareTo(max) > 0) {
            max = tmp;
        }
    }

    System.out.println(max);
}
2012-04-04 04:57
by Pau Kiat Wee


0

There is no specific library method for it, but you can do as below

Hashtable<Object, Double> hashTable = new Hashtable<Object, Double>();
          hashTable.put("a", 10.0);
          hashTable.put("b", 15.0);
          hashTable.put("c", 18.0);

          Collection<Double> values = hashTable.values();
          Double maxValue = Collections.max(values);
          Enumeration<Object> keys = hashTable.keys();
          while(keys.hasMoreElements()){
              Object key = keys.nextElement();
              if((hashTable.get(key)).equals(maxValue))
                  System.out.println(key);
          }
2012-04-04 05:01
by Chandra Sekhar


0

there is an important Catch-ya here: There could be more than one entry with the same MAX double value.

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;


public class HashtableTest {

    public static void main(String[] args){
        Hashtable<Object, Double> table = new Hashtable<Object, Double>();
    table.put("a", 10.0);
    table.put("b", 15.0);
    table.put("c", 18.0);
    table.put("d", 18.0);


        List<Object> maxKeyList=new ArrayList<Object>();
        Double maxValue = Double.MIN_VALUE; 
        for(Map.Entry<Object,Double> entry : table.entrySet()) {
             if(entry.getValue() > maxValue) {
                 maxValue = entry.getValue();
                 maxKeyList.add(entry.getKey());
             }
        }
        System.out.println("All max Keys : "+maxKeyList);
    }
}

result: All max Keys : [b, d]

2012-04-04 06:39
by Michael
Can I know, what happens if their are two highest values - Learner 2013-05-19 18:06
what I am suppose to do to find both values ke - Learner 2013-05-19 18:07
This finds all the keys that replace the old maximum value. With your test set it should have returned 'c' and 'd' not 'b' and 'd' - twain249 2013-05-19 20:26
your algorithm/code gives the wrong answer. As @twain249 said, it finds all keys that replace old max value. It should rather find a key which has max value or all the keys which have their value as max value - AnV 2018-01-14 07:11
Ads