java中map集合两种遍历方法

来源:互联网 发布:淘宝搜关键词在哪里 编辑:程序博客网 时间:2024/06/04 19:43

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() ;
}


本文出自 “ghost” 博客,请务必保留此出处http://caizi.blog.51cto.com/5234706/1559473

0 0