获得map中最大的value以及最大的key

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
public class MaxMapDemo {
 
public static void main(String[] args) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(18);
map.put(312);
map.put(553);
map.put(12333);
map.put(4211);
map.put(4442);
map.put(153);
 
System.out.println(getMaxKey(map));
System.out.println(getMaxValue(map));
 
}
 
/**
* 求Map<K,V>中Key(键)的最大值
* @param map
* @return
*/
public static Object getMaxKey(Map<Integer, Integer> map) {
if (map == nullreturn null;
Set<Integer> set = map.keySet();
Object[] obj = set.toArray();
Arrays.sort(obj);
return obj[obj.size()-1];
}
 
/**
* 求Map<K,V>中Value(值)的最大值
* @param map
* @return
*/
public static Object getMaxValue(Map<Integer, Integer> map) {
if (map == nullreturn null;
Collection<Integer> c = map.values();
Object[] obj = c.toArray();
Arrays.sort(obj);
return obj[obj.size()-1];
}
 
}
posted @ 2017-08-08 14:48  飞教主  阅读(21178)  评论(0编辑  收藏  举报