给定一个 hashMap 最终输出最大值的键

 1 /**
 2  * 
 3  * 类  描  述:机试题: 给定一个 hashMap 最终输出最大值的键
 4  * 作        者: 赵         鹏
 5  * 时        间:2017年7月4日 下午6:51:06
 6  */
 7 
 8 public class Test {
 9 
10     public static void main(String[] args) {
11         
12         Map<Integer, Integer> hashMap = new HashMap<Integer , Integer>();
13         
14         //给定一个hashmap
15         hashMap.put(1, 45);
16         hashMap.put(2, 6666);
17         hashMap.put(3, 15);
18         hashMap.put(4, 100);
19         hashMap.put(5, 3210);
20         
21         //输出最大值的键
22         System.out.println(getMaxKey(hashMap));
23         
24     }
25     
26     public static String getMaxKey(Map<Integer , Integer> hashMap) {
27         
28         int key   = 0;
29         int value = 0;
30         
31         int flagKey   = 0;
32         int flagValue = 0;
33         
34         Set<Entry<Integer,Integer>> entrySet = hashMap.entrySet();
35         
36         for (Entry<Integer, Integer> entry : entrySet) {
37             
38             //key value 代表每轮遍历出来的值
39             key   = entry.getKey();
40             value = entry.getValue();
41             
42             if(flagValue < value ) {
43                 
44                 //flagKey flagValue 当判断出最大值是将最大值赋予该变量
45                 flagKey   = key;
46                 flagValue = value;
47                 
48             }
49             
50         }
51         
52         return String.valueOf(flagKey);
53     }
54     
55 }
原文地址:https://www.cnblogs.com/zhao-blog/p/7149960.html