android關於HashMap取最大的value,並找出key。

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 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章