Hashtable中最大值的关键 [英] Key for maximum value in Hashtable

查看:141
本文介绍了Hashtable中最大值的关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有以下对象:

Hashtable<Object, Double>

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

and I want to find the key of the maximum Double value in the table. Easiest way to do that?

谢谢

推荐答案

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

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

编辑:查找最多1个以上的密钥值

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

这篇关于Hashtable中最大值的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆