zoukankan      html  css  js  c++  java
  • java中一个Map要找到值Value最小的那个元素的方法

    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];
        }
     
    }
  • 相关阅读:
    Installing PHP-7 with Memcached
    在Ubuntu1.4下升级php和Yii2
    apache设置反向代理实现前端js跨域访问
    mysql多重排序判断,根据状态区分时间排序方式
    利用缓存锁解决接口连续访问
    phpstorm启动内存配置
    ubuntu ssh修改用户密码
    Yii2手动安装第三方扩展(转)
    html input file 设置文件类型
    yii2 gridView中使用下拉列表筛选数据
  • 原文地址:https://www.cnblogs.com/zhanglingfei/p/6650753.html
Copyright © 2011-2022 走看看