java Hashtable 中最大值的键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10005053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Key for maximum value in Hashtable
提问by chopchop
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?
我想在表中找到最大 Double 值的键。最简单的方法来做到这一点?
Thanks
谢谢
回答by twain249
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.
没有内置函数来获取最大值,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();
}
}
Edit: To find more than 1 key for the max value
编辑:为最大值找到 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());
}
}
回答by st0le
If it's really important you do it without iterating all keys, simply extend HashTable
如果在不迭代所有键的情况下执行它真的很重要,只需扩展 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);
}
}
回答by Michael
there is an important Catch-ya here: There could be more than one entry with the same MAX double value.
这里有一个重要的问题:可能有多个条目具有相同的 MAX 双精度值。
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]
结果:所有最大键:[b, d]
回答by Pau Kiat Wee
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);
}
回答by Chandra Sekhar
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);
}