Map集合
1.根据value获取对应key
value不存在重复现象下可以使用
/**由value获取key*/
public static String getKeyByValue(Map map, Object value) {
String keys="";
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object obj = entry.getValue();
if (obj != null && obj.equals(value)) {
keys=(String) entry.getKey();
}
}
return keys;
}
运行结果:
2.获取Map集合value的最大值
Map集合的value需要为Integer类型
public static Object getMaxValue(Map<String, Integer> map) {
if (map == null)
return null;
int length =map.size();
Collection<Integer> c = map.values();
Object[] obj = c.toArray();
Arrays.sort(obj);
return obj[length-1];
}
运行结果:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。