用Java对LinkedHashMap进行排序 [英] Sorting a LinkedHashMap in Java

查看:1067
本文介绍了用Java对LinkedHashMap进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按值对HashMap进行排序. 我目前正在使用以下代码

I want to sort a HashMap by values. I'm currently using the following code

public static Map<String, Float> sortByValue(Map<String, Float> unsorted_map){

    Map<String, Float> sorted_map = new LinkedHashMap<String, Float>();

    try{
        // 1. Convert Map to List of Map
        List<Map.Entry<String, Float>> list = new LinkedList<Map.Entry<String, Float>>(unsorted_map.entrySet());

        // 2. Sort list with Collections.sort(), provide a custom Comparator
        Collections.sort(list, new Comparator<Map.Entry<String, Float>>() {
            public int compare(Map.Entry<String, Float> o1,
                    Map.Entry<String, Float> o2) {
                return (o1.getValue()).compareTo(o2.getValue());
            }
        });

        // 3. Loop the sorted list and put it into a new insertion order Map LinkedHashMap
        for (Map.Entry<String, Float> entry : list) {
            sorted_map.put(entry.getKey(), entry.getValue());
        }

    }
    catch(Exception e){
        e.printStackTrace();
    } 

    return sorted_map;

}

但是输出错误. 特别是,值以降序开始,然后在0.0值之后按升序排列(如第二个数据列所示).

but the output is wrong. In particular, values begin in descending order then, after a 0.0 value, they become in ascending order (as the second data column shows).

---> 1985 0.54660827
---> 2002 0.45290402
---> 1993 0.40605184
---> 2015 0.3591997
---> 2000 0.34358233
---> 1980 0.2811128
---> 2008 0.24987808
---> 1990 0.23426071
---> 1983 0.23426071
---> 1986 0.21864335
---> 1977 0.18740855
---> 1992 0.1717912
---> 2009 0.14055647
---> 2006 0.12493904
---> 2011 0.031234872
---> 2001 0.015617436
---> 1975 0.0
---> 1997 0.031234723
---> 2004 0.046852008
---> 1988 0.062469445
---> 2013 0.07808688
---> 1978 0.07808688
---> 2005 0.1093216
---> 1981 0.14055632
---> 1976 0.14055632
---> 2014 0.14055632
---> 1999 0.15617377
---> 1994 0.20302577
---> 1987 0.20302577
---> 2010 0.2186432
---> 1991 0.24987794
---> 1989 0.24987794
---> 1996 0.28111264
---> 2012 0.28111264
---> 2003 0.29673007
---> 1998 0.29673007
---> 2007 0.31234753
---> 1984 0.3904344
---> 1982 0.42166913
---> 1979 0.5309907
---> 1995 0.73401666

有人可以帮助我吗?

提前谢谢!

推荐答案

我认为您的代码应该可以工作.我像您一样编写代码,并且可以正常工作,这是我的代码

I think your code some should works. i write code like you and it works, here is my code

public static void main(String[] args) {
    Map<Integer, Float> unsortFloatMap = new HashMap<Integer, Float>();
    unsortFloatMap.put(1985, 0.54660827f);
    unsortFloatMap.put(2002, 0.45290402f);
    unsortFloatMap.put(1993, 0.40605184f);
    unsortFloatMap.put(2015, 0.3591997f);

    unsortFloatMap.put(1975, 0.0f);
    unsortFloatMap.put(1997, 0.031234723f);
    unsortFloatMap.put(2004, 0.046852008f);
    unsortFloatMap.put(2005, 0.1093216f);
    unsortFloatMap.put(2003, 0.29673007f);
    unsortFloatMap.put(1998, 0.29673007f);
    unsortFloatMap.put(2007, 0.31234753f);
    unsortFloatMap.put(1995, 0.73401666f);

    System.out.println("Unsort Float Map......");
    printMap(unsortFloatMap);

    System.out.println("Sorted Float Map......");
    Map<Integer, Float> sortedFloatMap = sortByFloatValue(unsortFloatMap);
    printMap(sortedFloatMap);
}

// pretty print a map
public static <K, V> void printMap(Map<K, V> map) {
    for (Map.Entry<K, V> entry : map.entrySet()) {
        System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
    }
}

private static Map<Integer, Float> sortByFloatValue(Map<Integer, Float> unsortMap) {
    List<Map.Entry<Integer, Float>> list = new LinkedList<Map.Entry<Integer, Float>>(unsortMap.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<Integer, Float>>() {
        public int compare(Map.Entry<Integer, Float> o1, Map.Entry<Integer, Float> o2) {
            return (o1.getValue()).compareTo(o2.getValue());
        }
    });

    /// Loop the sorted list and put it into a new insertion order Map
    /// LinkedHashMap
    Map<Integer, Float> sortedMap = new LinkedHashMap<Integer, Float>();
    for (Map.Entry<Integer, Float> entry : list) {
        sortedMap.put(entry.getKey(), entry.getValue());
    }

    return sortedMap;
}

输出为:

Unsort Float Map......
Key : 2002 Value : 0.45290402
Key : 2003 Value : 0.29673007
Key : 1985 Value : 0.54660827
Key : 1975 Value : 0.0
Key : 2007 Value : 0.31234753
Key : 2004 Value : 0.046852008
Key : 2005 Value : 0.1093216
Key : 1995 Value : 0.73401666
Key : 1993 Value : 0.40605184
Key : 1998 Value : 0.29673007
Key : 2015 Value : 0.3591997
Key : 1997 Value : 0.031234723
Sorted Float Map......
Key : 1975 Value : 0.0
Key : 1997 Value : 0.031234723
Key : 2004 Value : 0.046852008
Key : 2005 Value : 0.1093216
Key : 2003 Value : 0.29673007
Key : 1998 Value : 0.29673007
Key : 2007 Value : 0.31234753
Key : 2015 Value : 0.3591997
Key : 1993 Value : 0.40605184

这篇关于用Java对LinkedHashMap进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆