4

こんにちは私は次のオブジェクトを持っています:

Hashtable<Object, Double>

そして、テーブルで最大のDouble値のキーを見つけたいと思います。それを行う最も簡単な方法は?

ありがとう

4

5 に答える 5

7

から最大値を取得する組み込み関数はありません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();
     }
}

編集:最大値の複数のキーを見つけるには

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-04T04:56:13.283 に答える
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);
    }
}
于 2012-04-04T05:06:37.450 に答える
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);
}
于 2012-04-04T04:57:51.860 に答える
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);
          }
于 2012-04-04T05:01:48.170 に答える
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]

于 2012-04-04T06:39:25.853 に答える