zoukankan      html  css  js  c++  java
  • MAP按照value排序

    import java.util.Collections;
    import java.util.Comparator;
    import java.util.LinkedHashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import java.util.TreeMap;

    public class Test {
        public static Map<Integer, Double> Probs = new TreeMap<Integer, Double>();
        public static void main(String[] args) {
            Probs.put(1, 0.5);
            Probs.put(2, 1.5);
            Probs.put(3, 0.2);
            Probs.put(4, 10.2);
            Probs = sortByValueDescending(Probs);
            System.out.println("基于value值的降序,排序输出结果为:");
            for (Map.Entry<Integer, Double> entry : Probs.entrySet()) {
                System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
            }
            System.out.println();
            System.out.println("基于value值的升序,排序输出结果为:");
            Probs = sortByValueAscending(Probs);
            for (Map.Entry<Integer, Double> entry : Probs.entrySet()) {
                System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
            }
        }
        //降序排序
        public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueDescending(Map<K, V> map)
        {
            List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<K, V>>()
            {
                @Override
                public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2)
                {
                    int compare = (o1.getValue()).compareTo(o2.getValue());
                    return -compare;
                }
            });
      //另外一种写法:Collections.sort(list, (o1, o2) -> o2.getValue().compareTo(o1.getValue()));
            Map<K, V> result = new LinkedHashMap<K, V>();
            for (Map.Entry<K, V> entry : list) {
                result.put(entry.getKey(), entry.getValue());
            }
            return result;
        }
        //升序排序
        public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueAscending(Map<K, V> map)
        {
            List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<K, V>>()
            {
                @Override
                public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2)
                {
                    int compare = (o1.getValue()).compareTo(o2.getValue());
                    return compare;
                }
            });
      //另外一种写法:Collections.sort(list, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));
     
            Map<K, V> result = new LinkedHashMap<K, V>();
            for (Map.Entry<K, V> entry : list) {
                result.put(entry.getKey(), entry.getValue());
            }
            return result;
        }
    }
    参考:https://blog.csdn.net/qy20115549/article/details/79675007
  • 相关阅读:
    15个华丽的扁平风格的登录界面设计示例
    12款很酷的使用大头照的国外名片设计作品
    高清壁纸下载:15款精美的2014年元旦桌面壁纸
    Harp – 内置常用预处理器的静态 Web 服务器
    分享245款高质量的图标素材【免费下载】
    经典网页设计:关注用户体验的20个华丽网站
    使用 CSS3 实现超炫的 Loading(加载)动画效果
    你知道网页设计中最常用的15张图片素材吗?
    Koa – 更加强大的下一代 Node.js Web 框架
    Myth – 支持变量和数学函数的 CSS 预处理器
  • 原文地址:https://www.cnblogs.com/PureJava/p/10519207.html
Copyright © 2011-2022 走看看