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

 /**
*
* 类 描 述:机试题: 给定一个 hashMap 最终输出最大值的键
* 作 者: 赵 鹏
* 时 间:2017年7月4日 下午6:51:06
*/ public class Test { public static void main(String[] args) { Map<Integer, Integer> hashMap = new HashMap<Integer , Integer>(); //给定一个hashmap
hashMap.put(1, 45);
hashMap.put(2, 6666);
hashMap.put(3, 15);
hashMap.put(4, 100);
hashMap.put(5, 3210); //输出最大值的键
System.out.println(getMaxKey(hashMap)); } public static String getMaxKey(Map<Integer , Integer> hashMap) { int key = 0;
int value = 0; int flagKey = 0;
int flagValue = 0; Set<Entry<Integer,Integer>> entrySet = hashMap.entrySet(); for (Entry<Integer, Integer> entry : entrySet) { //key value 代表每轮遍历出来的值
key = entry.getKey();
value = entry.getValue(); if(flagValue < value ) { //flagKey flagValue 当判断出最大值是将最大值赋予该变量
flagKey = key;
flagValue = value; } } return String.valueOf(flagKey);
} }
上一篇:通过配置hosts.allow和hosts.deny文件允许或禁止ssh或telnet操作


下一篇:u-boot移植(十二)---代码修改---支持DM9000网卡