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];
        }
     
    }
  • 相关阅读:
    LeetCode 79
    LeetCode 437
    LeetCode 783
    LeetCode 59
    LeetCode 每日一题 04/24
    LeetCode 5
    LeetCode 43
    简易多线程任务 往数据库插数据
    定时任务--查数据库--注解实现
    redis 简易 实现
  • 原文地址:https://www.cnblogs.com/zhanglingfei/p/6650753.html
Copyright © 2011-2022 走看看