首页 > 编程语言 > 详细

java中一个Map要找到值Value最小的那个元素的方法

时间:2017-03-31 12:53:44      阅读:484      评论:0      收藏:0      [点我收藏+]

import java.util.Arrays;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
public class MinMapDemo {
 
    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(getMinKey(map));
        System.out.println(getMinValue(map));
         
    }
 
    /**
     * 求Map<K,V>中Key(键)的最小值
     * @param map
     * @return
     */
    public static Object getMinKey(Map<Integer, Integer> map) {
        if (map == nullreturn null;
        Set<Integer> set = map.keySet();
        Object[] obj = set.toArray();
        Arrays.sort(obj);
        return obj[0];
    }
 
    /**
     * 求Map<K,V>中Value(值)的最小值
     * @param map
     * @return
     */
    public static Object getMinValue(Map<Integer, Integer> map) {
        if (map == nullreturn null;
        Collection<Integer> c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return obj[0];
    }
 
}

java中一个Map要找到值Value最小的那个元素的方法

原文:http://www.cnblogs.com/zhanglingfei/p/6650753.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!