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];
        }
     
    }
  • 相关阅读:
    时间复杂度计算
    SQL Server2012编程入门经典(第四版) 读书笔记
    一些编程试题
    Qt 对话框显示控制按钮
    vc++创建文件目录
    配置ubuntu虚拟机备忘
    Qt QThread 多线程使用
    Qt 程序等待多长时间执行Sleep
    Qt 数字和字符处理总结
    c++ 文件utf-8格式
  • 原文地址:https://www.cnblogs.com/zhanglingfei/p/6650753.html
Copyright © 2011-2022 走看看