在Java中对LinkedHashMap进行排序

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

public static Map sortByValue(Map unsorted_map){ Map sorted_map = new LinkedHashMap(); try{ // 1. Convert Map to List of Map List<Map.Entry> list = new LinkedList<Map.Entry>(unsorted_map.entrySet()); // 2. Sort list with Collections.sort(), provide a custom Comparator Collections.sort(list, new Comparator<Map.Entry>() { public int compare(Map.Entry o1, Map.Entry 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 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 

有人可以帮帮我吗?

提前致谢!

我认为你的代码应该有用。 我像你一样编写代码并且它有效,这是我的代码

 public static void main(String[] args) { Map unsortFloatMap = new HashMap(); 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 sortedFloatMap = sortByFloatValue(unsortFloatMap); printMap(sortedFloatMap); } // pretty print a map public static  void printMap(Map map) { for (Map.Entry entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue()); } } private static Map sortByFloatValue(Map unsortMap) { List> list = new LinkedList>(unsortMap.entrySet()); Collections.sort(list, new Comparator>() { public int compare(Map.Entry o1, Map.Entry o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); /// Loop the sorted list and put it into a new insertion order Map /// LinkedHashMap Map sortedMap = new LinkedHashMap(); for (Map.Entry 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 

最合理的解释是您的打印代码无法打印-在负数前面。 您的排序代码是正确的。

排序代码没问题。 您的打印似乎失败了。 检查负数打印。

尝试使用Stream API进行此排序

 LinkedHashMap 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”碰撞解析器。

 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 names = new HashSet(); 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 tempList = new ArrayList(names); Collections.sort(tempList); System.out.println("HashSet element in sorted order : " + tempList); // Sorting HashSet using TreeSet TreeSet sorted = new TreeSet(names); System.out.println("HashSet sorted using TreeSet : " + sorted); } }