如果取出HashMap的所有key以及对应的值

解决方案 »

  1.   

    HashMap map = new HashMap();
    map.put("1","aaaa");
    map.put("2","bbbb");
    System.out.println(map.keySet());
    System.out.println(map.get("1"));
    运行结果:
    [2, 1]
    aaaa
      

  2.   

    HashMap<String,String> map = new HashMap<String,String>();
    map.put("1", "1");
    map.put("2", "2");
    for (Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator(); iter.hasNext();) {
    Map.Entry<String, String> entry = iter.next();
    System.out.println(entry.getKey() + " : " + entry.getValue());

    }
      

  3.   

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;public class Hmcode {  
    public static void main(String[] args) {
     
         HashMap hm=new HashMap();
         
    hm.put("1","one");
    hm.put("2","two");
    Set set=hm.entrySet();
    Iterator iit=set.iterator();
    while(iit.hasNext()){
    Map.Entry me=(Map.Entry)iit.next();
    System.out.println(me.getKey()+" 对应的值是:"+me.getValue()) ;
    }
         }
      }
      

  4.   

    谢谢!
    请问如何全部打印出来某个DTO里面的所有属性的值?