Hashtable中最大值的关键

时间:2012-04-04 04:43:06

标签: java hashtable max

您好我有以下对象:

Hashtable<Object, Double>

我希望找到表中最大Double值的键。最简单的方法吗?

由于

5 个答案:

答案 0 :(得分:6)

没有内置函数可以从Hashtable中获取最大值,您必须遍历所有键并手动确定最大值。

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();
     }
}

编辑:为最大值

找到1个以上的键
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());
     }
}

答案 1 :(得分:4)

如果在没有迭代所有键的情况下执行此操作非常重要,只需扩展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);
    }
}

答案 2 :(得分:0)

您可以循环查找最大值:

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);
}

答案 3 :(得分:0)

没有特定的库方法,但您可以按照以下方式执行

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);
          }

答案 4 :(得分:0)

这里有一个重要的Catch-ya:可能有多个条目具有相同的MAX double值。

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);
    }
}

结果:所有最大键:[b,d]