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

JAVA中Map聚合遍历的方法

2012-08-21 
JAVA中Map集合遍历的方法1.声明一个map: Map map new HashMap() 2.向map中放值,注意:map是key-value的

JAVA中Map集合遍历的方法
1.声明一个map:

Map map = new HashMap(); 

2.向map中放值,注意:map是key-value的形式存放的.如:

map.put(”sa”,”dd”);


3.从map中取值:String str = map.get(”sa”).toString();结果是:
str = ”dd”;


4.遍历一个map,从中取得key 和value

JDK1.5

Map m = new HashMap(); for (Object o : map.keySet()) {   map.get(o); }

JDK1.4

Map map = new HashMap() ; Iterator it = map.entrySet().iterator() ; while (it.hasNext()) {   Map.Entry entry = (Map.Entry) it.next() ;   Object key = entry.getKey() ;   Object value = entry.getValue() ; }

热点排行