HashMap中取得key值和value值(另:HashMap会自己排序的问题)

// 第一种:通过Map.keySet遍历key和value 
   System.out.println("通过Map.keySet遍历key和value:"); 
    for (String key : map.keySet()) { 
         System.out.println("key= " + key + "  and  value= " + map.get(key)); 
    } 


//第二种
  private static Map temp = new HashMap();

  for(Iterator it = temp.entrySet().iterator(); it.hasNext(); ){
     Map.Entry e = (Map.Entry)it.next();
     System.out.println("key: " + e.getKey());
     System.out.println("value: " + e.getValue());

  } 
 

 

 

你可能感兴趣的:(Java编程)