首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

遍历地图的几种方法

2012-09-21 
遍历map的几种方法public class Test{?public static void main(String[] args) {MapString,String map

遍历map的几种方法

public class Test{?

public static void main(String[] args) {

Map<String,String> map = new HashMap<String,String>();?

map.put("id1", "wang");?

map.put("id2", "li");?

?

//map的遍历方法1?

Set<String> set = map.keySet();?

for(String s:set){ System.out.println(s + "," + map.get(s)); }?

?

//map的遍历方法2?

Set<Map.Entry<String, String>> entryseSet = map.entrySet();?

for(Map.Entry<String, String> entry:entryseSet){ System.out.println(entry.getKey()+","+entry.getValue()); }?

?

//map的遍历方法3?

Iterator<String> it = map.keySet().iterator();?

while(it.hasNext()){ System.out.println(map.get(it.next())); }?

?

//map的遍历方法4?

Iterator it = map.values().iterator();?

while(it.hasNext()){?

String val = (String)it.next();?

System.out.println(val);?

}?

}

}

热点排行