从map中取出最大或最小value对应的key---多种写法
2024-08-31 21:19:15
package com.yuwanlong.hashing; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; /**
* @author Yu Wanlong
*/ public class ValidAnagram {
public static void main(String[] args) {
Map<String, Double> map = new HashMap();
map.put("1", 8.);
map.put("2", 12.);
map.put("3", 53.);
map.put("4", 33.);
map.put("5", 11.);
map.put("6", 3.);
map.put("7", 3.);
map.put("8", 1.);
//List<Entry<String,Integer>> list = new ArrayList(map.entrySet());
//Collections.sort(list, (o1, o2) -> (o1.getValue() - o2.getValue()));
String minKey = getMapMinOrMaxValueKey(map, "min");
String maxKey = getMapMinOrMaxValueKey(map, "max");
System.out.println(map.get(minKey));
System.out.println(map.get(maxKey));
} public static String getMapMinOrMaxValueKey(Map<String, Double> map, String choose) {
List<Entry<String,Double>> list = new ArrayList(map.entrySet());
Collections.sort(list, (o1, o2) -> (o1.getValue().intValue() - o2.getValue().intValue()));
String key = "";
if (choose.equals("min")) {
key = list.get(0).getKey();
} else if (choose.equals("max")) {
key = list.get(list.size() - 1).getKey();
}
return key;
}
}
最新文章
- 我的EF功能
- 腾讯AlloyTeam移动Web裁剪组件AlloyCrop正式开源
- perl 从文件里读出变量无法使用解决办法
- 供应商和管理员查看供应商地址簿信息SQL
- The file couldn’t be opened because you don’t have permission to view it
- 502 Bad Gateway nginx 解决
- Js跳出循环
- oracle的存储过程语法(转)
- Sublime中开发Ruby
- 自定义checkbox, radio样式
- 数据保存策略(Retention Policies)
- 斐讯K1 K2 开启Telnet
- 日志那点事儿——slf4j源码剖析
- 通过数组和枚举简化GPIO操作编码(转)
- 如何利用好github的问题
- 线性查找算法(BFPRT)
- PHP的数据加密解密
- mes平台Action类模版
- 【Unity】6.4 Transform--移动、旋转和缩放游戏对象
- 一个 图片 滚动 飞入的css特效