您的位置:首页 > 其它

map的两种取值方式

2016-02-26 21:08 435 查看
public class MapUtil{
public static void iteratorMap1(Map m) {
Set set=m.keySet();//用接口实例接口
Iterator iter = set.iterator();
while (iter.hasNext()) {//遍历二次,速度慢
String k=(String)iter.next();
System.out.println(k +"="+ m.get(k));
//System.out.println(iter.next()+"="+ m.get(iter.next()));
//因为指针判断下一个有没有值 iter.next是当前对象 但是 m.get(iter.next())是下一个值
}
public static void iteratorMap2(Map m){
Iterator i=m.entrySet().iterator();
while(i.hasNext()){//只遍历一次,速度快
Map.Entry e=(Map.Entry)i.next();
System.out.println(e.getKey()+"="+e.getValue());
//System.out.println(e.setValue(""));//返回value的值
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: