Author:斗阑干_Null 2018/7/29
如何将Map键值对中的键和值单独取出来呢?
public class MapDemo{
public static void main(String []args){
Map countries = new HashMap();
countries.put("苏","江苏");
countries.put("鲁","山东");
countries.put("湘","湖南");
}
}
方法一:(先获取每个key键,通过.get()方法,得到相应的value值)
1、增强型for循环
Set keys = countries.keySet();//调用.keySet()方法取出所有key,返回为一个Set类型集合
//keys中取出来的key,默认为Object类型
for(Object obj:keys){
//将取出来的key,强制转换为字符串类型
String key = (Sting)obj;
//调用.get()方法,获取map中的值
String value = (String)countries.get(key);
System.out.println(key+"\t"+value);
}