在Java中排序LinkedHashMap



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

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值之后,它们变为升序(如第二列数据所示)。

---> 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

有人能帮帮我吗?

提前感谢!

排序代码可以。你的打印似乎失败了。检查数字打印

尝试使用流API

进行排序
LinkedHashMap<String, Integer> map 
    = unsorted_map.entrySet()
            .stream()
            .sorted(Map.Entry.comparingByValue(Integer::compareTo))
            .collect(Collectors.toMap(
                Map.Entry::getKey,
                Map.Entry::getValue,
                (a, b) -> a,
               LinkedHashMap::new
            ));

收集器使LinkedHashMap节点与"take one"冲突解析器同一性

我认为你的代码有些应该工作。我像你一样写代码,这是我的代码

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

最合理的解释是您的打印代码未能打印出负数前面的-。您的排序代码是正确的

package com.JavaExample;
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.HashSet; 
import java.util.List; 
import java.util.TreeSet;
public class HashSetSorted 
{
    public static void main(String[] args)
    {
        HashSet<String> names = new HashSet<String>(); 
        names.add("Asker");
        names.add("Crak"); 
        names.add("Bayliss"); 
        names.add("Mohna"); 
        names.add("Dina");
        System.out.println("HashSet before sorting : " + names); 
        // Sorting HashSet using List 
        List<String> tempList = new ArrayList<String>(names);
        Collections.sort(tempList); 
        System.out.println("HashSet element in sorted order : " + tempList);     
        // Sorting HashSet using TreeSet
        TreeSet<String> sorted = new TreeSet<String>(names);
        System.out.println("HashSet sorted using TreeSet : " + sorted);
    }
}

最新更新