一、for 循環遍歷map集合中的元素,entrySet()方法。網絡
public class Test{get
public static void mian(String[]args){it
HashMap <String,String> hm=new HashMap<String,String>();class
hm.put("a","aaaa");循環
hm.put("b","bbbb");遍歷
hm.put("c","cccc");map
Iterator it=hm.entrySet().iterator();//返回鍵值對應的Set試圖集合 方法
while(it.hasNext()){next
Map.Entry entry=(Map.Entry)it.next();static
Object key=entry.getKey();
Object value=entry.getValue();
System.out.println("鍵="+key+"值="+value);
}
/*//forEach循環遍歷
for (Map.Entry<String, Integer> entry : hm.entrySet())
{
String key = entry.getKey().toString();
String value = entry.getValue().toString();
System.out.println("key=" + key + " value=" + value);
}
*/
}
}
二、根據map集合的key值來獲取相對應的value
public class Test1{
public static void mian(String[]args){
HashMap <String,String> hm=new HashMap<String,String>();
hm.put("a","aaaa");
hm.put("b","bbbb");
hm.put("c","cccc");
Iterator it=hm.keySet().iterator();
while(it.hasNext()){
Object key=it.next();
Object value=hm.get(key);
System.out.println("key=" + key + " value=" + value);
}
}
}
Note:自行導包,鏈接的遠程網絡編寫的。手動的。