2013-11-09 33 views
1

我已定义为一个HashMap如下在一个HashMap排序基于的ArrayList <String>的大小

Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); 

我然后存储从DATABSE数据在此HashMap和显示内容在控制台上如下,其中朝向所述条目留下的--->是ID和向右项是由该ID

165767--->[dual-boot, windows, uninstall, ati, graphics, multiple-monitors] 
6873 --->[kubuntu, re-installation] 
34228--->[11.10, unity, launcher, libreoffice, icons] 

我想在下降的基础上,他们已经使用即基于地图标签数顺序编号的排序中使用的标签。获得(key).size()以便输出应该是ID 165767,然后是34228,然后是6873等等。

我试着用TreeMap这样做,但我无法弄清楚如何根据大小而不是按键的值,而且按降序排列。

+0

[HashMap的排序由值]的可能重复(http://stackoverflow.com/questions/8119366/sorting-hashmap-by-values)。另外,您需要一个比较器,根据它们的长度比较列表。 –

+0

您可以先制作地图,然后再进行排序吗? –

+0

或者...如何获得已经从数据库中排序的数据,您可以简单地将其转换为HashMap?你应该可以用SQL来做到这一点。 – scottb

回答

0

编辑

我的输出应该基于标签数量和 ID

Map<String, ArrayList<String>> map = new TreeMap<String, ArrayList<String>>(); 


map.put("165767",new ArrayList<String>(Arrays.asList("dual-boot", "dual-boot", "windows", "uninstall", "ati", "graphics", "multiple-monitors"))); 
map.put("6873",new ArrayList<String>(Arrays.asList("kubuntu", "kubuntu", "re-installation"))); 
map.put("0000000000000000",new ArrayList<String>(Arrays.asList("test","test", "test"))); 
map.put("0125",new ArrayList<String>(Arrays.asList("dual-boot", "windows", "uninstall", "ati", "graphics", "multiple-monitors"))); 


for(ArrayList<String> l : map.values()){ 
    Set<String> hs = new HashSet<>(); 
    hs.addAll(l); 
    l.clear(); 
    l.addAll(hs); 
} 

List<ArrayList<String>> l = new ArrayList<>(map.values()); 
Collections.sort(l, new Comparator<ArrayList<String>>(){ 
    public int compare(ArrayList<String> s1, ArrayList<String> s2){ 
     return Integer.compare(s2.size(), s1.size());     
    }}); 

for(ArrayList<String> a : l){ 
    Iterator<Entry<String, ArrayList<String>>> iter = map.entrySet().iterator(); 
    while (iter.hasNext()) { 
     Entry<String, ArrayList<String>> e = iter.next(); 
     if(e.getValue().equals(a)){ 

      System.out.println(e.getKey() + "-" + a); 
      iter.remove(); 
     } 
    } 
} 

输出的不是大小进行排序:

0125-[uninstall, dual-boot, graphics, windows, ati, multiple-monitors] 
165767-[uninstall, dual-boot, graphics, windows, ati, multiple-monitors] 
6873-[re-installation, kubuntu] 
0000000000000000-[test] 
+0

我认为你误解了这个问题。我的输出应该根据标签的数量而不是ID的大小排序。你的输出看起来像是根据ID的大小进行排序。 – user2916886

+0

@ user2916886检查我的编辑。这是你在找什么? –

+0

是的,这是我一直在寻找。谢谢你的帮助。只是更喜欢。假设我的标签有可能重复。如何将它们从最终显示中删除? – user2916886

3

这创建了一个排序的ID列表。

List<String> sortedIds = new ArrayList<String>(map.getKeys()); 
Collections.sort(sortedIds, new Comparator<String>() { 
    public int compare(String a, String b) { 
     return map.get(b).size() - map.get(a).size(); 
    } 
}); 

不是说你永远不会保持SortedMap(如TreeMap),上排序可变值(如一个ArrayList的长度)。由于排序顺序用于查找值,因此如果"id123"变得大于"id456"而没有收集知道它,则可能会导致非常大的问题。

0

我有类似的情况和此代码为我工作(有时我必须空):

 private static Map<Object,List<Object>> sortByArraySizeDesc(Map<Object,List<Object>> map) { 
     List<List<Object>> list = new LinkedList(map.entrySet()); 
     Collections.sort(list, new Comparator() { 
       public int compare(Object o1, Object o2) { 
        if (o1 == null && o2 == null) { return 0; } 
        else if (o1 == null) { return 1;} 
        else if (o2 == null) { return -1; } 
        int size1 = ((List) ((Map.Entry) (o1)).getValue()).size(); 
        int size2 = ((List) ((Map.Entry) (o2)).getValue()).size(); 
        return size2 - size1; 
       } 
     }); 

     Map res = new LinkedHashMap(); 
     for (Iterator it = list.iterator(); it.hasNext();) { 
      Map.Entry entry = (Map.Entry)it.next(); 
      res.put(entry.getKey(), entry.getValue()); 
     } 
     return res; 
    } 
相关问题