日期:2014-05-20  浏览次数:20753 次

如何遍历Map对象获取值?
我有一个Map对象,在不知道其键的情况下,如何遍历这个Map对象取得所有的值?

------解决方案--------------------
Iterator iterator=map.keySet().iterator();
while(iterator.hasNext()){
Object key=iterator.next();
Object value=map.get(key);
System.out.println(key+ "-- "+value);
}
先获取key值的集合,再根据key一一获取value
------解决方案--------------------
楼上完全正确。
------解决方案--------------------
没错呀,这种东东,看看API就成了,不要出来问啊,呵呵.


<!-- Search Google -->
<center>
<form method= "get " action= "http://www.google.cn/custom " target= "_top ">
<table bgcolor= "#ff3333 ">
<tr> <td nowrap= "nowrap " valign= "top " align= "left " height= "32 ">

<label for= "sbi " style= "display: none "> 输入您的搜索字词 </label>
<input type= "text " name= "q " size= "31 " maxlength= "255 " value= " " id= "sbi "> </input>
<label for= "sbb " style= "display: none "> 提交搜索表单 </label>
<input type= "submit " name= "sa " value= "Google 搜索 " id= "sbb "> </input>
<input type= "hidden " name= "client " value= "pub-2645657960589265 "> </input>
<input type= "hidden " name= "forid " value= "1 "> </input>
<input type= "hidden " name= "ie " value= "GB2312 "> </input>
<input type= "hidden " name= "oe " value= "GB2312 "> </input>
<input type= "hidden " name= "cof " value= "GALT:#003324;GL:1;DIV:#66CC99;VLC:FF6600;AH:center;BGC:C5DBCF;LBGC:73B59C;ALC:000000;LC:000000;T:330033;GFNT:333300;GIMP:333300;FORID:1 "> </input>
<input type= "hidden " name= "hl " value= "zh-CN "> </input>
</td> </tr> </table>
</form>
</center>
<!-- Search Google -->

------解决方案--------------------
for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry)it.next();
System.out.println(entry.getKey() + " = " + entry.getValue());
}
------解决方案--------------------
接分
------解决方案--------------------
for(Map.Entry <String, String> entry : map.entrySet()) {
  System.out.println(entry.getKey() + " ==> " + entry.getValue());
}
------解决方案--------------------
接分
------解决方案--------------------
首先,bao110908(长牙了,好痛)的方法更好。
另外,忘了Map是否有values方法了,如果有的话,那么这个方法返回的集合就是你想要的集合。