1、for 循环遍历map集合中的元素,entrySet()方法。
public class Test{
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.entrySet().iterator();//返回键值对应的Set试图集合
while(it.hasNext()){
Map.Entry entry=(Map.Entry)it.next();
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);
}
*/
}
}
2、根据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:自行导包,连接的远程网络编写的。手动的。