有一个HashMap对象,其中key是一个string对象,value是一个Integer对象,要求写一个方法,输出HashMap对象的值,形式为:key   -- value 
(我想要按顺序输出HashMap中的值(有序),请各位热心人在此代码上修改,最好能在各位机子上运行通过的全部代码,谢谢!)方法1:
public class HashMapTest {
    
    public static void printPL(Map hm) {
        Set s = hm.keySet();        
        Iterator i = s.iterator();
        while(i.hasNext()) {
            Object o = i.next();
            System.out.println(o +" -- "+hm.get(o));
        }
    }
    
    public static void main(String[] args) {
        HashMap hm = new HashMap();
        hm.put("ha0",new Integer(1000));
        hm.put("ha1",new Integer(1001));
        hm.put("ha2",new Integer(1002));
        hm.put("ha3",new Integer(1003));
        
        printPL(hm);
    }
}方法2:public   class   HashMapTest   {         public   static   void   printPL(Map   hm)   { 
                Set   s   =   hm.entrySet();                 
                Iterator   i   =   s.iterator(); 
                while(i.hasNext())   { 
                        Map.Entry   o   =   (Map.Entry)i.next(); 
                        System.out.println(o.getKey()   +"   --   "+o.getValue()); 
                } 
        }         public   static   void   main(String[]   args)   { 
                HashMap   hm   =   new   HashMap(); 
                hm.put("hao",new   Integer(1000)); 
                hm.put("ha1",new   Integer(1001)); 
                hm.put("ha2",new   Integer(1002)); 
                hm.put("ha3",new   Integer(1003));                 printPL(hm); 
        } 
}

解决方案 »

  1.   

    使用TreeMap,自动排序!!!!!
      

  2.   


    public   class   HashMapTest   {         private HashMap map;        HashMapTest(HashMap map){ this.map = map ; }        public   void   printPL(){ 
                    SortedMap<String,Integer> tree = 
                             new TreeMap<String,Integer>(new Comparator<String>() 
                             {                                                 
    public int compare(String a,String  b){ return map.get(a).intValue - map.get(b).intValue;}
                             })
                    tree.addAll(map);
                    Set  s =  tree.entrySet();                                   
                    Iterator  i = s.iterator();   
                    while(i.hasNext()){   
                          Map.Entry o = (Map.Entry)i.next();   
                          System.out.println(o.getKey()+"--"+o.getValue());   
                                    }   
            } 
            
            public   static   void   main(String[]   args)   { 
                    HashMap   hm   =   new   HashMap(); 
                    hm.put("ha0",new   Integer(1000)); 
                    hm.put("ha1",new   Integer(1001)); 
                    hm.put("ha2",new   Integer(1002)); 
                    hm.put("ha3",new   Integer(1003)); 
                    
                    new HashMapTest(hm).printPL(); 
            } 

    用的treemap  不知道有什么错误没有! 我没有运行
      

  3.   

    public   class   HashMapTest   { 
            
            public   static   void   printPL(Map   hm)   { 
                    Set   s   =   hm.keySet();                 
                    Iterator   i   =   s.iterator(); 
                    while(i.hasNext())   { 
                            Object   o   =   i.next(); 
                            System.out.println(o   +"   --   "+hm.get(o)); 
                    } 
            } 
            
            public   static   void   main(String[]   args)   { 
                    TreeMap   hm   =   new   TreeMap(); 
                    hm.put("ha3",new   Integer(1003));
                    hm.put("ha0",new   Integer(1000));
                    hm.put("ha2",new   Integer(1002)); 
                    hm.put("ha1",new   Integer(1001)); 
                    printPL(hm); 
            } 
    }
      

  4.   

    不用TreeMap,还有什么其它方法没有,用ArrayList,collections.sort()怎么具体实现?需要实现的全部代码,谢谢,因为我是菜鸟!
      

  5.   

    TreeMap是按照key来排序的.import java.util.*;
    public       class       HashMapTest       {                   public       static       void       printPL(Map       hm)       {   
                                    Set       s       =       hm.entrySet();                                   
                                    Iterator       i       =       s.iterator();   
                                    while(i.hasNext())       {   
                                                    Map.Entry       o       =       (Map.Entry)i.next();   
                                                    System.out.println(o.getKey()       +"       --       "+o.getValue());   
                                    }   
                    }                   public       static       void       main(String[]       args)       {   
                                    TreeMap       hm       =       new       TreeMap(); 
                                    //hm.put("hao",new       Integer(1000)); //你这里写o了,没有写0,我帮你改了。
                                    hm.put("ha0",new       Integer(1000));   
                                    hm.put("ha3",new       Integer(1003));   
    hm.put("ha1",new       Integer(1001));
    hm.put("ha2",new       Integer(1002));
                                    printPL(hm);   
                    }   
    }
      

  6.   

    你去看API吧,map和ArrayList是不一样的东西.Map是按照key,value存值的,ArrayList是数组的形式,它是实现Collection接口的.key,value存值是实现Map接口的.
      

  7.   

    把KEY放到TREEMAP里或者任何有排序功能的Array里.排序.然后到你的HashMap里面去get 
    不知道为什么一定要用HashMap
      

  8.   

    http://springjava.javaeye.com/blog/656465  参考例子