如何排序hashmap? [英] How to sort hashmap?
问题描述
我有散列表,它的关键是像文件夹/ 1.txt,文件夹/ 2.txt,文件夹/ 3.txt和值有这些文本文件的数据。
现在我被困住了。我想对这个列表进行排序。但它不会让我这样做:(
这是我的hashmap数据类型:
HashMap< String,ArrayList< ;字符串>>
以下函数可以很好地工作,但它是用于arraylist而不是hashmap。
Collections.sort(values,Collections.reverseOrder());
我也尝试过MapTree,但它也没有工作,或者可能是我无法使它工作。我使用
按以下步骤使用maptree对代码进行排序
HashMap testMap = new HashMap();
Map sortedMap = new TreeMap(testMap);
以其他方式做到这一点??
我有一个怀疑我的钥匙是(文件夹/1.txt,文件夹/ 2.txt)可能是这样的原因?
如果你的散列表叫做 h
,那么试试这个:
SortedSet< String> sortedK eys = new TreeSet< String>(h.keySet());
I have hashmap and its keys are like "folder/1.txt,folder/2.txt,folder/3.txt" and value has these text files data.
Now i am stucked. I want to sort this list. But it does not let me do it :( Here is my hashmap data type:
HashMap<String, ArrayList<String>>
following function work good but it is for arraylist not for hashmap.
Collections.sort(values, Collections.reverseOrder());
I also tried MapTree but it also didn't work, or may be I couldn't make it work. I used following steps to sort the code with maptree
HashMap testMap = new HashMap();
Map sortedMap = new TreeMap(testMap);
any other way to do it??
I have one doubt as my keys are (folder/1.txt, folder/2.txt ) may be that's why?
I guess that you want the list of keys sorted.
If your hashmap is called h
, then try this:
SortedSet<String> sortedKeys = new TreeSet<String>(h.keySet());
这篇关于如何排序hashmap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!