HashMap遍历与排序

资料整理
HashMap<String,Integer> word_map = new HashMap<String,Integer>();
...
int length = word_map.size();
Iterator iter =word_map.entrySet().iterator();
if(iter != null){
         while(iter.hasNext()){
        Entry element = (Entry)iter.next();
        Object strKey = element.getKey(); //键值
        Object strValue = element.getValue(); //value值
        }
}
HashMap<String,Integer> word_map = new HashMap<String,Integer>();
...
List list = new ArrayList(word_map.entrySet());
Collections.sort(list,
         new Comparator(){
         public int compare(Object o1,Object o2){
                Entry obj1 = (Entry) o1;
                Entry obj2 = (Entry) o2;
                 return ((Integer)(obj2.getValue()) - (Integer)(obj1.getValue()));
}});
for(Iterator iter = list.iterator();iter.hasNext();){
        Entry element = (Entry) iter.next();
        Object strKey = element.getKey(); //键值
        Object strValue = element.getValue(); //value值
}

你可能感兴趣的:(职场,休闲,资料整理)