给定一个 hashMap 最终输出最大值的键
2024-08-29 04:24:52
/**
*
* 类 描 述:机试题: 给定一个 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);
} }
最新文章
- C/C++头文件使用 #ifndef #define #endif 的原因
- 从Bayesian角度浅析Batch Normalization
- liunx 多个tomcat 产生的新问题
- 《DSP using MATLAB》示例Example4.11
- [工作中的设计模式]适配器模式adapter
- HTML5标签及使用方法描述
- 得到指定进程PID
- mahout入门指南之基于mahout的itembased算法
- HDU-2058-The sum problem(数学题技巧型)
- django-个人博客登录及权限验证功能的实现
- Java collection 容器
- nginx与PHP配置
- 依赖注入容器之Castle Windsor
- apt 下载安装包
- hibernate5(12)注解映射[4]一对一外键关联
- Unicode编码:保存中文cookie
- 切换svn登录账户
- Error creating bean with name &#39;sessionFactory&#39; defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable t
- 天梯赛 L2-024. (并查集) 部落
- Linux下文件的打包、解压缩指令——tar,gzip,bzip2,unzip,rar