importjava.util.Arrays;
importjava.util.Collection;importjava.util.HashMap;importjava.util.Map;importjava.util.Set;public classMinMapDemo { publicstaticvoidmain(String[] args) { Map<Integer, Integer> map = newHashMap<Integer, Integer>(); map.put(1, 8); map.put(3, 12); map.put(5, 53); map.put(123, 33); map.put(42, 11); map.put(44, 42); map.put(15, 3); System.out.println(getMinKey(map)); System.out.println(getMinValue(map)); } /** * 求Map<K,V>中Key(键)的最小值 * @param map * @return */ publicstatic Object getMinKey(Map<Integer, Integer> map) { if(map == null) returnnull; Set<Integer> set = map.keySet(); Object[] obj = set.toArray(); Arrays.sort(obj); returnobj[0]; } /** * 求Map<K,V>中Value(值)的最小值 * @param map * @return */ publicstaticObject getMinValue(Map<Integer, Integer> map) { if(map == null) returnnull; Collection<Integer> c = map.values(); Object[] obj = c.toArray(); Arrays.sort(obj); returnobj[0]; }}
A map in Java to find the element whose value is the least