android关于HashMap<String, Integer>取最大的value,并找出key。

来源:互联网 发布:中日军机对峙知乎 编辑:程序博客网 时间:2024/05/21 09:45

HashMap<String, Integer> map = new HashMap<String, Integer>();

Iterator iter = map.entrySet().iterator();

int num = 1;

HashMap<String, Integer> tempMap = new HashMap<String, Integer>(1);
String tempKey = null;
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String key = (String) entry.getKey();
int val = (Integer) entry.getValue();
if(val<100)
{
if(tempMap != null && tempKey != null){
if(val < tempMap.get(tempKey)) {
tempKey = key;
tempMap.put(key, val);
}
}
if(tempKey == null){
tempKey = key;
tempMap.put(key, val);
}
num = Math.max(val, num);
}

}

最大的value为num,对应的key为tempKey 

0 0
原创粉丝点击