zoukankan      html  css  js  c++  java
  • Collections工具获取最大值和最小值

    1.对于Collection的数据,可以使用Collections工具获取最大值和最小值。最大值和最小值的下标可以通过List的indexOf方法获取。
    2.对于Map的数据,可以先将Map的value转成Collection,然后通过Collections工具获取最大值和最小值。至于最大值和最小值的下标只能通过遍历来获取。
    
    private void getMaxAndMin()
    {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        Integer listMax = Collections.max(list);
        Integer listMin = Collections.min(list);
        Log.i("getMaxAndMin","listMax:"+listMax+" listMin:"+listMin);
        Log.i("getMaxAndMin","listMax index:"+list.indexOf(listMax)+" listMin index:"+list.indexOf(listMin));
        Map<Integer,Integer> map = new HashMap<>();
        map.put(0,1);
        map.put(1,2);
        map.put(2,3);
        map.put(3,4);
        map.put(4,5);
    
        Collection<Integer> collection = map.values();
        Integer mapMax = Collections.max(collection);
        Integer mapMin = Collections.min(collection);
        Log.i("getMaxAndMin","mapMax:"+mapMax+" mapMin:"+mapMin);
    
        Integer mapMaxIndex = null;
        Integer mapMinIndex = null;
        Set set = map.entrySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext())
        {
            Map.Entry entry = (Map.Entry) iterator.next();
            if (entry.getValue().equals(mapMax)) {
                mapMaxIndex = (Integer) entry.getKey();
            }
            if (entry.getValue().equals(mapMin)) {
                mapMinIndex = (Integer) entry.getKey();
            }
        }
        Log.i("getMaxAndMin","mapMaxIndex:"+mapMaxIndex+" mapMinIndex:"+mapMinIndex);
    }
    
      
    
  • 相关阅读:
    浏览器内核
    为什么一般请求可以下载文件,Ajax 请求就不能下载
    转 深入理解javascript原型和闭包(10)——this
    node读取文本文件时,去掉BOM
    AMD & CMD
    gulp requirejs Error: ENOENT: no such file or directory, open '/js/require_config.js', 一直报找不到require_config.js,坑死了
    [BZOJ3224]普通平衡树
    [NOIP2014D2]
    [NOIP2014D1]
    [NOIP2013D2]
  • 原文地址:https://www.cnblogs.com/xiaoniuniu886/p/11451979.html
Copyright © 2011-2022 走看看