2016-09-16 123 views
0

我想按值排序HashMap。 我正在使用下面的代码在Java中对LinkedHashMap排序

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 

有人可以帮我吗?

在此先感谢!

+0

为什么不利用这种种本身一个TreeMap? – puhlen

+0

@ puhlen因为它按键排序,而不是按值排序。 –

+0

这个实现对我来说看起来是正确的。实际上,我真的怀疑你用来打印地图的代码,或者在你调用这个方法时不想分配排序后的地图。 –

回答

1

我认为你的代码应该有效。我写喜欢你的代码和它的作品,这里是我的代码

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 
1

排序代码正常。您的打印似乎失败。检查负数打印号码。

使用流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节点与“走一个”碰撞解析。

1

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

0
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); 
    } 

} 
+0

请修复代码格式。在每行上缩进4个空格以构成一个代码块。 – jingx